Reputation: 1332
I have a jar which contains another jar, my goal is to run this "inside jar" from the global jar.
I have this method which retrieves the full path of the jar contained in the jar
private static String getRealPathFromResourceName(String fileName) throws IOException {
ClassLoader cl = Main.class.getClassLoader();
URL resource = cl.getResource(fileName);
String path = resource.getPath();
return path;
}
Then I run this :
public static boolean exportFullPeriod(CheckConfiguration checkConfig) throws IOException {
final String cmdLine = buildExportCommandLine(checkConfig);
try {
Process process = Runtime.getRuntime().exec(cmdLine);
....
} catch (
...
}
where build exportCommandLine builds the full command line using the previous method, the full output is :
java -jar file:/C:/Users/xxx/target/module-backup-1.0.jar!/export-1.1.jar -dbUrl jdbc:mysql://localhost:3306/test -dbPassword test -dbUser test
The error I get is the following
Error: Unable to access jarfile file:/C:/Users/xxx/target/module-backup-1.0.jar!/export-1.1.jar
How can I reference the jar inside my jar to call it ?
Upvotes: 1
Views: 801
Reputation: 4598
The java process does not know how to extract a jar from a jar and run a class in the inner jar. You will need to extract it to a directory where the outer jar is running (a dynamic variable). Then create the command line based on that and finally run it.
So if you have /u/myapps/app.jar that has a entry "someDir/anotherApp.jar" First extract that to a directory, say "/u/myapps/tmp/someDir/anotherApp.jar", then run it from here.
See java.util.zip package for how to open a jar (or zip) file from java.
if you do not have write permission to "/u/myapps/tmp/" then try using the tmp entry from System.getProperties()
Upvotes: 2