new File("config.ini");

1. When used via eclipse, the correct file is found in the root of the project.
2. When used via a runnable jar, it looks in C:\users\ME\config.ini

How do I make it look at the file outside of the jar?

New Folder:
- program.jar
- config.ini

Because I want the config to be easy to modify....

It looks like new File("config.ini") will look for "config.ini" in the current working directory..... I was in C:\users\ME\ when i ran it via its file path which was in another folder.

When I switch to the folder the jar was in, it worked fine. Any way to make it use the path the jar is in?
I believe System.getProperty("user.dir") is the same as the current working dir so that wont work.

Edited 6 Years Ago by TheWhite: n/a

It looks like new File("config.ini") will look for "config.ini" in the current working directory..... I was in C:\users\ME\ when i ran it via its file path which was in another folder.

When I switch to the folder the jar was in, it worked fine. Any way to make it use the path the jar is in?
I believe System.getProperty("user.dir") is the same as the current working dir so that wont work.

Correct. You don't specify WHERE to look for the file so it looks for it in the current working folder.
You can supply the folder to look for in the constructor for File.
That folder can be specified as a commandline parameter to the application at program startup which you can simply read out using the standard mechanisms provided to do that.

C:\users\ > java -jar C:\users\desktop\new folder\test.jar

System.getProperty("user.dir") returns c:\users\

You would think the working dir would change since the files in the jar are running, but it doesnt.

"user.dir" would work if double clicking the jar file, but not when executing from a different working dir. I need a sure way to do it and the only way I can think of is to find the location of the .jar file.

Any other suggestions?

Edited 6 Years Ago by TheWhite: n/a

This article has been dead for over six months. Start a new discussion instead.