Current directory path when run jar

Related searches

My program read configuration data by reading xml file fro current directory:

File fXmlFile = new File("configFile.xml");

It works fine in NetBeans IDE. I have build project and got jar file. It runs fine if I double click on it in Windows 10. In case I open file by using right click on jar and Open with -> Java program can't find configuration file. In this case I got exception:

java.io.FileNotFoundException: C:\Windows\System32\configFile.xml (The system cannot find the file specified)

Why it looks just to system path and not in current directory? How to ask program to load file in current directory when running in Open with -> Java case?

Jar's manifest file:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.10.4
Created-By: 12.0.1+12 (Oracle Corporation)
Class-Path: lib/log4j-api-2.11.2.jar lib/log4j-core-2.11.2.jar lib/met
 ouia.jar lib/swt.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.aaa.myprog.runMe 

The best way for you to read config.xml and other assets your app may need is putting them in src/main/resources and then referencing them as files in your classpath, like this:

Shell
mv configFile.xml /users/vico/my_program/src/main/resources
Java Code
// ...

public static File getResourceAsFile(String resourcePath) {
    try {
        InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath);
        if (in == null) {
            return null;
        }

        File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
        tempFile.deleteOnExit();

        try (FileOutputStream out = new FileOutputStream(tempFile)) {
            //copy stream
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
        }
        return tempFile;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

File fXmlFile = getResourceAsFile("configFile.xml");

// ...

(this code was borrowed from this stackoverflow answer)

Then you can move your jar wherever you want, even ship it to your users and stop worrying about where the config files lie.

Setting the class path, The default class path is the current directory. Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored. jdk-dir >/classes directory as a class library will run in the current version, but there is no guarantee� // The path to the jar is the "bin" directory in that case because there is no actual .jar file. surroundingJar = jarDir; } else { // Combining the path and the name of the .jar file to achieve the final result surroundingJar = jarDir + jarFileFromSys.substring(1); } System.out.println("JAR File: " + surroundingJar);

The current directory is, as Victor already pointed out, dependent on the command that is used to launch the JVM and therefore dynamic at runtime. You instead need a locator that is dependent on the location on the jar file itself, meaning it is dynamic at compile time but static at runtime.

There are different approaches here, so let me shortly introduce two:

Use a launcher script This way you simply take control of the command line yourself, but you have to do it for every operating system where you plan to use your program. On Windows it could look like this:

app.bat:

cd %~dp0
java -jar app.jar

More information on the first line here.

Use System ClassLoader This works, because the System ClassLoader's sources are dynamic at compile time but static at runtime, so exactly what you need. However, it comes with the downside that you cannot write to the configuration file, as you only get an InputStream.

app.jar

try (InputStream fXml = ClassLoader.getSystemClassLoader().getResourceAsStream("configFile.xml")) {
    ...
}

And a full MCVE.

ConfigFile.java:

public class ConfigFile {
    public static void main(String[] args) {
        try (final BufferedReader configFile = new BufferedReader(
            new InputStreamReader(ClassLoader.getSystemClassLoader()
                .getResourceAsStream("configFile.txt")))) {
            System.out.println(configFile.readLine());
        } catch (final IOException exc) {
            exc.printStackTrace();
        }
    }
}

ConfigFile.txt

Hello World

MANIFEST.MF

Manifest-Version: 1.0
Class-Path: .
Main-Class: prv.izruo.test.ConfigFile

command line

P:\workspace\ConfigFile>dir deploy
...
02.05.2019  20:43             1.434 configFile.jar
02.05.2019  20:43                11 configFile.txt

P:\workspace\ConfigFile>java -jar deploy\configFile.jar
Hello World

Adding Classes to the JAR File's Classpath (The Java™ Tutorials , Class-Path: jar1-name jar2-name directory-name/jar3-name can avoid having to specify a long -classpath flag when invoking Java to run the your application. I need the path to the jar not the currently executing path. Itay's code snipplet gives me my current working directory (the execution directory).

As i see the problem arises because you need to know how relative paths are resolved at exectution time, and the "current directory" takes place on this ecuation, so allow me to explain a little bit.

As far i know the "current directory" is also knows as working directory and it is set when you launch a java application and it assumes the value of the directory from where you execute the java command to launch the app.

So for example:

If you open a command terminal, and you place yourself in a directory "C:\pepe" and from whitin that directory you execute the command to launch your jar, e.g.: java -jar "c:\path\to\my\app.jar" then the working directory would be "c:\pepe\" and inside your program any reference to relative path would be completed using the "c:\pepe\" prefix.

And them the line File fXmlFile = new File("configFile.xml"); will instruct the jvm to look the file at "c:\pepe\configFile.xml", as the relative path configFile.xml will be resolved using the working directory (set previously at launch-time) as i explained above.

Having in mind how java sets the working directory and how relative paths are resolved using the working directory as prefix, help you solve any reference problem, from now onwards!

You may find this link useful: Getting the Current Working Directory in Java for investigating what is the working diretory set by netbeans when it launch (for us) the java app.

TD;DR:

"pepe" is the spanish term equals to "foo" in the english term :D, hope to learn something new!! :D

Create A Stand-Alone Java Application, Now we want to change to the directory/folder that contains your compiled code. Now we'll create a single JAR file containing all of the files needed to run your � Whenever you are working with files in Java, sometimes you need to get the full path to the directory the .jar file is located in. Most of the time you can just use.String path = "./File_Name.txt"; // Note the "." before the "/".

How to set CLASSPATH in Java, (dot) , meaning that only the current directory is searched for dependencies. Let's say we have a folder named dependency where JAR files and other classes are placed. Executing programs with '-classpath' or '-cp' option in Java. Often the 'current directory', a concept common to most environments, is on the classpath; if your current directory is c:\test\, and com is in that directory, and "." (the symbol for the current directory) is in the class path, then you can run your program and it can import and use Matrix.

How to get the current working directory in Java, jar. But when i generate my project to .jar and run it. Default working directory set to my home folder “/home/[username]/” instead of� how can we include all the jar files within a directory in the same classpath? My attempt: java -classpath lib/*.jar:. my.pack.Code1 and it is not able to find class files that are certainly in those jars.

Since you don't explicitly specify a the path part the java executable looks for the jar file in the current working directory. And since that is where the jar file is it finds it. c:\javaWork>java -jar client.jar The current working directory for this command is "c:\javaWork" and there is no jar file there. So it doesn't run.

Comments
  • Where does the configuration file reside, relative to your jar file?
  • Jar and configuration file are in the same directory
  • I prefer not use any startup scrip I choose second approach. But I got null in fXml
  • In case I put configFile.xml inside of jar file everything goes fine. How to ask system get resources not from jar, but from same directory where jar file is located?
  • Honestly, I have no idea. Today I re-checked my project at work and composed an MCVE at home. Both are perfectly fine.
  • @vico I think I found it studying the Manifest of my MCVE: You must add . to your classpath (or whatever folder you wish to save your config file to).
  • "If you execute c:\pepe\java -jar "c:\path\to\my\app.jar" the working directory would be "c:\pepe\"" —That is not correct. You can make any directory the current working directory using cd, then execute that same command. The working directory is not defined by the location of the executing program, it’s a property of the individual process or parent command shell.
  • thanks for pointing that out, as far i know without any aditional configuration your working directory defaults to the directory from where you execute the command. If there is any aditional way to set it, please, enlight us, feel free to edit my post in order to enumarate other ways to set the working directory.
  • cd C:\pepe is how you change the working directory.
  • ahhh understand what you are saying, allow me to fix my post
  • @VGR thanks for suggestion, i have edit the post; have you any other observation? again: feel free to edit my post