Current path

M

miki.miska

Hi !

How can I obtain current path of the main class (or JAR file) ? Perhaps
by using args from the 'main' function or there is some ENVIRONMENT set
by jvm ?

thx
 
J

John C. Bollinger

How can I obtain current path of the main class (or JAR file) ?

Why do you want to do? Whatever you have in mind, if it's appropriate
to do at all then there is probably a better way to go about it.
Perhaps
by using args from the 'main' function or there is some ENVIRONMENT set
by jvm ?

The most straightforward, reasonably generic option I can think of is:

String mainClassName
= MyMainClass.class.getName().replaceAll("\\.", "/");
URL mainClassLocation
= MyMainClass.class.getClassLoader().getResource(mainClassName);

Adjust for your needs, and note that there is some chance that this
would fail -- or produce a URL of unexpected form -- if your main class
were loaded in an unusual manner.
 
J

John C. Bollinger

John said:
The most straightforward, reasonably generic option I can think of is:

String mainClassName
= MyMainClass.class.getName().replaceAll("\\.", "/");

Oops. Make that

String mainClassName
= MyMainClass.class.getName().replaceAll("\\.", "/")
+ ".class";
 
M

miki.miska

Oops. Make that

String mainClassName
= MyMainClass.class.getName().replaceAll("\\.", "/")
+ ".class";
I need this as a location for storing default config file for the
application. I also offer -D option etc..., but I would like anyway to
search for the configuration file inside of the main class directory.
 
H

HK

(e-mail address removed) wrote:
[about the path of the main class or jar file of an app]
I need this as a location for storing default config file for the
application. I also offer -D option etc..., but I would like anyway to
search for the configuration file inside of the main class directory.

There are several things to keep in mind here:
1) Use the config file only read-only as your app might
be installed by an admin and a user it will not
be able to write to the file.

2) When guessing a directory from a file name, keep
in mind that it may be a (soft) link, which means your
app is actually installed in a completely different place.

3) When you deliver an default config in your jar
file, then just use java.lang.ClassLoader getResourceAsStream().
I would expect this to work even if the jar is unpacked
and the config file changed in place by an admin during
installation.

Harald.
 
J

John C. Bollinger

I need this as a location for storing default config file for the
application. I also offer -D option etc..., but I would like anyway to
search for the configuration file inside of the main class directory.

Your application does not explicitly need to know the directory in order
to find its config file if that file is anywhere in the classpath (which
it will be if it is in the same directory as the main class). Simply
ask the main class' ClassLoader to getResource() (to get the URL) or
getResourceAsStream() (to get it as a stream). Note that these will
work even if the file is _inside_ the application's jar, though the file
will be read-only in that case.

This is exactly the kind of thing I meant when I said that there would
be a better way to go about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top