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

Dani AI

Generated

Quick summary and a reliable fix.

correctly noticed that a relative filename is resolved against the process working directory, so launching the jar from a different shell leaves the JVM looking in that shell's current folder. 's suggestion to accept an explicit folder argument or startup property is a good, explicit solution. If you prefer an automatic default that points to the folder that actually contains the running JAR, compute that folder from the class location and use it to resolve your config file.

A robust pattern is: ask the class' protection domain for its code source location, convert the returned URL to a URI (this handles spaces and encoding), turn that into a Path, and if the path points at a JAR file take its parent directory; if it points at a classes directory (IDE run) use it directly. Example helper:

import java.nio.file.*;
import java.net.URI;

public static Path jarDir(Class<?> c) {
    try {
        URI uri = c.getProtectionDomain().getCodeSource().getLocation().toURI();
        Path p = Paths.get(uri);
        return Files.isRegularFile(p) ? p.getParent() : p;
    } catch (Exception e) {
        // fallback: safe default (e.g. current working dir or an explicit startup property)
        return Paths.get(".").toAbsolutePath();
    }
}

Caveats: codeSource can be null or restricted by a SecurityManager, and special launchers or native installers may change behavior. Also, classpath resources (what pointed to) are useful when the config is packaged inside the JAR, but not when you want an editable external file. Best practice: allow an override (command-line argument or system property), then fall back to the JAR-folder resolution above, then to a final safe default. This covers double-click launches, command-line launches, and IDE runs.

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.