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

Recommended Answers

All 7 Replies

Why wouldn't you just package it in the jar?

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.

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.

How do I get the folder the jar file is in?

Try System.getProperty("user.dir");

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?

Class.getResource() will locate a resource based on the classpath.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.