Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can not get the absolute path of the running applet class

 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am currently writting an APplet program. I am using JRE 1.5 and WSAD 5.1.2 as my IDE. I tried to get the absolute path of the applet program that I am running. For example, I put my applet program inside my project folder under WSAD directory, such as D:/WSADworkspace/BookStore; I wrote the following code and I just want to get back the path.

Here is my code:



Unfortunately, I am not getting my path. I am getting a wrong path instead. It's C:\Documents and Settings\myName\Desktop, which is under USER HOME DIRECTORY of my JRE 1.5 console. I don't know why, and I wish I can know why, it's strange to me.

Please point out what's wrong with my code, and how can I get back my real absolute path.

myriads thanks

Transistor
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check Class File about it.

The class.getName() will return class name without path name.
 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really? so what's the proper way to do it ?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only want to know the working directory the you can try System.getProperty("user.dir").
 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
poor me, I am still getting non sense directory "C:\Documents and Settings\Ken\Desktop" as my result, however my actual file path is "D:\WSADworkspace\BookStore". why is that ? why ? I am getting crazy
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a way to get the file location of the .class file for a class under some circumstances -- but running in an applet is not going to be one of them. Especially given that in a deployment situation, the class file is going to be on one machine's disk, while the applet is running on an entirely different machine!

In any case, "new File(X)" where X is anything that doesn't look like an absolute path is going to get you a path relative to the current working directory, which is apparently your desktop directory. The right way to get the file path to a .class file is to use ClassLoader.getResource("my/class/Class.class") which gives you a "file:" URL for a local class (unless, of course, Class.getClassLoader() returns null for the class, or the security manager blocks the call.)

OK, so, here's the important question: why do you need to do this? What are you trying to accomplish? Maybe there's a better way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic