Board index » jbuilder » location of jar file

location of jar file


2006-10-20 07:14:03 PM
jbuilder21
Hi,
i have a question concerning the location of the jar file.
In Delphi there is the application object, and the property
ExeName gives the location of the exe. In Java there is no
application object. But i still would like to know where the jar
file is located.
At the moment im using the following approach:
- my installation routine writes an environment variable (content: the path into which the installation writes the jar)
- my program reads this environment variable
This works, but i do not really like this approach.
What i really would appreciate is an information from the class
itself (where it is located) - without any foreign info (such
as environment variable).
At the moment my jar file doesnt know where it is, it needs somebody else to tell him :)
regards
Oliver
 
 

Re:location of jar file

Oliver wrote:
Quote
Hi,

i have a question concerning the location of the jar file.
In Delphi there is the application object, and the property
ExeName gives the location of the exe. In Java there is no
application object. But i still would like to know where the jar
file is located.

At the moment im using the following approach:
- my installation routine writes an environment variable (content: the path into which the installation writes the jar)
- my program reads this environment variable

This works, but i do not really like this approach.
What i really would appreciate is an information from the class
itself (where it is located) - without any foreign info (such
as environment variable).

At the moment my jar file doesnt know where it is, it needs somebody else to tell him :)

I'm not sure if this solves the problem but when I run the following:
Properties prop = new Properties();
prop = System.getProperties();
String dir=prop.getProperty("user.dir");
System.out.println("dir="+dir);
the "user.dir" property display where I run my program
To see all properties:
prop.list(System.out);
 

Re:location of jar file

Oliver wrote:
Quote
i have a question concerning the location of the jar file.
In Delphi there is the application object, and the property
ExeName gives the location of the exe. In Java there is no
application object. But i still would like to know where the jar
file is located.
String myClassName = MyClass.getName();
String myClassFileName = myClassName.replace('.', '/') + ".class";
URL whereAmI = MyClass.class.getResource(myClassFileName);
Fix any syntax errors. I've used this general approach before, but I
haven't actually compiled or tested the exact code shown above.
If the class is in a JAR file, this will be a JAR URL. See the
documentation on java.net.JarURLConnection for more details. If the
class file comes from somewhere else (a directory with class files, a
network URL, etc.) then you would need to handle that differently.
--
Gillmer J. Derge [TeamB]
 

{smallsort}

Re:location of jar file

Hi
thanks a lot. This works during design time.
I will try it also for a jar file and i will have a look at
this JAR url class.
friendly greetings
Oliver
 

Re:location of jar file

Hi
thanks, im just wondering what the difference would be
between your approach and the other approach using the url of
the main class. Hmm... :)
friendly greetings
Oliver
 

Re:location of jar file

Oliver wrote:
Quote
Hi

thanks, im just wondering what the difference would be
between your approach and the other approach using the url of
the main class. Hmm... :)

friendly greetings

Oliver
I wonder about the same thing. Please keep us posted.