Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java app launch dir?

Reply
Thread Tools

Java app launch dir?

 
 
Michael Brown
Guest
Posts: n/a
 
      07-29-2004
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


 
Reply With Quote
 
 
 
 
KC Wong
Guest
Posts: n/a
 
      07-29-2004
> 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);
}
}


 
Reply With Quote
 
 
 
 
Liz
Guest
Posts: n/a
 
      07-29-2004

"KC Wong" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> > 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);
> }
> }
>


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.


 
Reply With Quote
 
Alan Moore
Guest
Posts: n/a
 
      07-29-2004
On Wed, 28 Jul 2004 21:12:05 -0500, "Michael Brown" <(E-Mail Removed)>
wrote:

>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("/")));

 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      07-29-2004
Michael Brown wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ App to Run in DOS and Launch Another App tempnode@gmail.com C++ 6 10-21-2008 07:47 PM
Launch a new java app from a java program Yao Qi Java 30 07-04-2007 02:12 AM
How do I launch a new Java app from a running Java app? steve.albin@gmail.com Java 3 01-03-2007 04:59 PM
Launch java app (jar) from c++ carsten.marx@gmail.com Java 3 11-02-2005 02:25 PM
How can i block java web start from downloading jre .. but still launch app Glenn M Java 3 10-29-2004 10:36 AM



Advertisments