Search code examples
javajarpathsystemexecution

Launching jar from java: execution path and relative resources


I'm trying to run a jar file from Java code, through exec().

The jar I'm executing have some resources relative to its path that need to be loaded. So for example executing from console: java -jar [/path/to/jar/]exec.jar is working only if the command is launched from the same directory

I've tried with: cd /path/to/jar/exec.jar && java -jar /path/to/jar/exec.jar but it seems there is a problem within exec() for running cd, widely covered on the web. The main problem is that I'm looking for a procedure that runs both on Linux and Windows.

I've tried to mess with the -classpath option, but with no luck.

Is there any simple solution to this? Note that I'm not "fond" of the system call idea, it's just I was looking for a simple way to schedule the execution of custom jars.

Thank you in advance! CB


Solution

  • You need to set the working directory of the launched process using ProcessBuilder.directory(File).