Reputation: 1234
I created a JAR file with my Java program. This piece of code will open a few files inside a dir "Test", which is in the same dir as the JAR file. Like this:
/
-- program.jar
-- /Test
-- *
If I run via terminal with: java -jar program.jar, it runs perfectly. But if I run graphically (right clicking on the jar file and Open with OpenJDK...), it doesn't work properly. Just like if I ran from another directory.
Is it possible that when I run the JAR file graphically it's running from another directory?
By the way, I'm running on Ubuntu.
Upvotes: 0
Views: 2269
Reputation: 43504
Yes, you will get another current working directory... There would be two solutions:
1) Find the cwd by doing this hack:
public class Test {
public static void main(String... args) {
ClassLoader cl = Test.class.getClassLoader();
String f = cl.getResource("").getFile();
File cwd = new File(f);
if (cwd.toString().endsWith("!"))
cwd = cwd.getParentFile();
JOptionPane.showMessageDialog(null, cwd);
}
}
2) If the files under Test
are static (does not change to often) the solution would be to package them inside the jar.
Upvotes: 2
Reputation: 3735
I'm not sure if this will help, but below is a link to similar JAR question.
Running jar file with source files in another directory
Upvotes: 0