1

I have a .jar file that I want to execute in java. The .jar file is located inside the main project directory. How can I do that

public void pickColor (ActionEvent event) throws IOException, InterruptedException {
    String filePath = "pickColor.jar";
    Runtime.getRuntime().exec(" java -jar " + filePath);
}

If I add the full path of the file it works ...

Andrei Paciurca
  • 433
  • 7
  • 17

2 Answers2

0

The most realiable alternative I think is to write the jar to temp file. Some thing like:

    File file = File.createTempFile("pickColor", ".jar");
    FileUtils.copyURLToFile(getClass().getResource(filePath), file);
    Runtime.getRuntime().exec(" java -jar " + filePath);

FileUtils is from apache common-io.

ThomasEdwin
  • 2,035
  • 1
  • 24
  • 36
0

you can try the below code

public void pickColor (ActionEvent event) throws IOException, InterruptedException {
   String fileName = "pickColor.jar";
   String filePath = System.getProperty("user.dir");
   Runtime.getRuntime().exec(" java -jar " + filePath + "\" + fileName);
}
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74