Search code examples
javaexelaunch4j

How to extract file from wrapped Jar in Java


The file (exe) is located inside JAR file, which was wrapped to EXE using Launch4j.

My executable (JAR file wrapped in EXE):

myProgram.exe

Executable location

myProgram.exe/program.jar/resources/sub-program.exe

How to run (or extract to temp directory) sub-program.exe and run it with parameters? sub-program.exe -f -q

If the sub-program was a file in temp, I could run it as:

String exeLocation = System.getProperty("java.io.tmpdir") + "sub-program.exe";
String params = " -f -q";    
ProcessBuilder process = new ProcessBuilder("cmd.exe", "/c", exeLocation + params);

How to extract file from wrapped Jar file ? I guess there is no way to run it inside wrapped Jar file.


Solution

  • You can get InputStream for resource in classpath, by something like getClass().getResourceAsStream("/resources/sub-program.exe"). You can read from the stream and create a file at any place you like (e.g. under temp directory as you hinted), and execute it.