• Post Reply Bookmark Topic Watch Topic
  • New Topic

Relative path with JAR files  RSS feed

 
Thomas Kiersted
Greenhorn
Posts: 22
Opera Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm not sure quite where to put this, so I chose general. But the gist of my problem is, I have a program that I package in a JAR file, and I want to be able to access a configuration .ini file outside of the JAR file - so the user can configure it themselves easily. But I can't get the relative paths working to access the .ini file.

My .jar file is and the associated directory is structured like this, according to 7Zip:

config.ini
MyProgram.jar
--Main.class
--Main$1.class
--Main$2.class
--Main$3.class
--Main$4.class
--Main$5.class
--53OtherClasses.class
--META-INF (directory)
----MANIFEST.MF

The Manifest contains:



I want to access config.ini from Main.class. In my source code, I've tried File config = new File("config.ini"); and File config = new File("../config.ini");, but both fail on my if (file.exists()) check. The only thing I can figure is that perhaps I need something else in my Class-Path? Such as the config.ini file itself? But then would File config = new File("config.ini"); work?

Currently testing on Windows, but I want it to run on any OS, so I'd rather use / than \. And when I specify an absolute path, / works, even on Windows. But I can't very well specify an absolute path and expect everyone to have the same file structure, especially as they're unlikely to all be running Windows. Thanks in advance for the help.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't specify an absolute path then the file is searched in the current directory. Example, if your .jar file is located in testdir1/testdir2, and you run java from testdir1 as current dir, then the file won't be found. If you "cd" to testdir2, and then run the program, the file will be found.

As a solution, you could try to put the location of the file into an environment variable, and access that with System.getenv(). Perhaps there are better solutions tho.
 
Thomas Kiersted
Greenhorn
Posts: 22
Opera Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Ogranos wrote:If you don't specify an absolute path then the file is searched in the current directory. Example, if your .jar file is located in testdir1/testdir2, and you run java from testdir1 as current dir, then the file won't be found. If you "cd" to testdir2, and then run the program, the file will be found.

As a solution, you could try to put the location of the file into an environment variable, and access that with System.getenv(). Perhaps there are better solutions tho.


You're right... it does default to whatever directory the .jar is in. Turns out my program was loading the config file correctly when I didn't specify a directory, and the problem was in the config file itself - I specified the path I wanted to use after loading the config file in quotes, and Java tried to change to that directory with quotes .

Question though, would not using an environment variable significantly increase the risk of decreased portabiliity? At least half the reason I'm using Java is the design-anywhere-debug-everywhere paradigm not being too bad, so I'm trying to avoid operations that might increase that problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!