• Post Reply Bookmark Topic Watch Topic
  • New Topic

JAR and filesystem  RSS feed

 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I posted this in JNLP and got no reply so I'm reposting because I'm really stuck on it.

My application is finished and now I want to make it easy for people to run. An executable jar file seems the simplest format. But I'm having problems with accessing the file system from the jar. For example, my program needs to read a setup file from a subdirectory of the one containing the jar. If the user clicks on the jar from his windowing system, I can I be sure that the jar knows where it is, or how can I specify filenames relative to the location of the jar, but not inside it?
 
John Kelty
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using System.getProperty("user.dir") provides the directory that your executable JAR is in when it gets launched by the user.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried this...but it doesn't quite work.
Under my Linux system, when I click on the jar, the System.getProperty("user.dir") returns my home directory /home/carthur
regardless of where I put the jar.

Chris
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you were using "user.dir" and not "user.home"? The first property is the current working directory and the second is the user home directory.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I have tried both of them. They both return the same thing.
Incidentally, the program works just fine if I run it from the command line with "java -jar myapp.jar"
But I'm running Gnome, and when I try to click on it from Gnome's file browser, the result is as I claimed.
 
John Kelty
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be a configuration issue with Gnome, but I've never used Gnome so I wouldn't know. I tried some experiments on Eclipse and, like you found, it always returns the IDE's current working directory.

Eclipse has a "Working Directory" setting so perhaps it is overriding the JAR location with that setting. Also possible is that Gnome and/or Eclipse is loading the file from the source code in a Java file instead of the JAR file, in which case the user.dir lookup is working as designed.

Can you make sure that there is no .java or .class files available to Gnome when you use it to launch the JAR, to make sure that the only place it can get the source is from the JAR file itself.

Of course that's all a lot of guesswork since I'm gnome-ignorant.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use eclipse also, and it always works fine when I run from there.

I'm not smart enough to diagnose Gnome, so I'm just going to throw up a dialog if it doesn't right the first time.

Or maybe I can just set the run command to include the path somehow,
like

java -Duser.dir=/good/path -jar myapp

Of course I guess the laziest solution is just to tell the user to run from the command line if it doesn't work from the windowing environment.
[ August 09, 2006: Message edited by: Christopher Arthur ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!