daniel gon
daniel gon

Reputation: 99

Desktop.getDesktop().open(file) doesn´t work if jar needs an external file

I hava a jar file file.jar that needs a folder with pictures next to where the jar is. The program loads the pictures and shows them.

It works fine doing double click or java -jar file.jar, but if I try to open it with another java program doing

File file = new File("path/file.jar");
Desktop.getDesktop().open(file)

it´s not loading the pictures.

What can it be?

Upvotes: 1

Views: 253

Answers (1)

Andreas
Andreas

Reputation: 159114

When you run java -jar file.jar, the jar file is in the current directory, and presumably so is the picture folder.

When you double-click a jar file, the code is run with the containing folder as the current directory.

When you "open" path/file.jar, the current directory is obviously different, because why else would you need to qualify the jar file name. Since you program replies on the current directory to find the picture folder, it fails.

Solutions, in my recommendation order (given the little I know of your code):

  1. Include the pictures inside the jar file, then access them using getResourceAsStream.

  2. Include the pictures in another jar file, have the manifest file of you main jar file include the picture jar file in the classpath.

  3. Make sure the current directory is correct before try to "open" the file.

Upvotes: 2

Related Questions