Java app launch dir?

M

Michael Brown

How can my standalone Java app discover what directory it lives in? I want
to have the app look in that directory for a default properties file. I
don't think this is necessarily what is returned by
System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
for what directory the jar is in.

Thanks....

Mike
 
K

KC Wong

How can my standalone Java app discover what directory it lives in? I
want
to have the app look in that directory for a default properties file. I
don't think this is necessarily what is returned by
System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
for what directory the jar is in.

If your properties file are in the same directory as the JAR, then you don't
need to know where exactly it is...

Take a look at the Java API docs, about java.lang.Class, methods
getResource() and getResourceAsStream().

e.g.

private static final String PROGRAM_CONFIG = "/config.ini";

class Foo {
public Foo() {
InputStream configStream =
getClass().getResourceAsStream(PROGRAM_CONFIG);
}
}
 
L

Liz

KC Wong said:
If your properties file are in the same directory as the JAR, then you don't
need to know where exactly it is...

Take a look at the Java API docs, about java.lang.Class, methods
getResource() and getResourceAsStream().

e.g.

private static final String PROGRAM_CONFIG = "/config.ini";

class Foo {
public Foo() {
InputStream configStream =
getClass().getResourceAsStream(PROGRAM_CONFIG);
}
}

I do something like this
File dir = new File(".");
but that gives me the directory where the "java" command was issued
not necessarily the same as where the jar file lives.
 
A

Alan Moore

How can my standalone Java app discover what directory it lives in? I want
to have the app look in that directory for a default properties file. I
don't think this is necessarily what is returned by
System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
for what directory the jar is in.

Thanks....

Mike
String s = MyApp.class.getResource("MyApp.class").getFile();
s = s.substring(5, s.indexOf("!"));
File installDir = new File(s.substring(0, s.lastIndexOf("/")));
 
J

Jacob

Michael said:
How can my standalone Java app discover what directory it lives in? I want
to have the app look in that directory for a default properties file. I
don't think this is necessarily what is returned by
System.getProperty("user.dir"), is it? BTW, app is in a jar, so I'm looking
for what directory the jar is in.

The common approach is to put the properties file
*inside* the jar. This works fine for site specific
deployment configuration etc. To ease the deployment
process, you might put it into a separate jar file
(app.jar + someCustomer.jar) which will be equivalent
as seen from your application.

This may however not be appropriate for all situations:
User preferences is updated per session, and should
live on the client machine. However, you have no
knowledge of the directory of your jar file, and it
is a bad idea to look there anyway. Using "user.dir"
is one option, but using the Preferences API from the
JDK is a lot better.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top