2 .jar files not interacting with each other
I have recently been adding an updater to my Java programm and all the code is working. The problem I am having however is that the initial program doesn't go on to run the second .jar file or even run the file in NetBeans and just shuts down instead.
I believe this bit of code to be the thing that isn't working but I have done a tonne of System开发者_Go百科.out's and it just runs straight through the code without running the second programme:
private void update() {
String[] run = {"java", "-jar", "updater/update.jar"};
try {
Runtime.getRuntime().exec(run);
} catch (Exception ex) {
ex.printStackTrace();
}
System.exit(0);
}
My folders/files are in this path:
LibTiles.jar (initial file that runs this code)
updater/update.jar
Can anyone see any errors or have I done something wrong in another place?
I believe java.exe is not in your PATH environment.
In addition, try to start java.exe, or better, javaw.exe with your file (add the .exe), like this:
String[] run = {"java.exe", "-jar", "updater/update.jar"};
If you don't want the console window, use:
String[] run = {"javaw.exe", "-jar", "updater/update.jar"};
The wording of your question is terribly confusing. You mention LibTiles.jar and you post a snippet that runs the main method in updater/update.jar. Does the code in update.jar require .class files in LibTiles.jar?
I can think of two possible problems, both caused by the lack of a manifest in your update.jar. It has to say what the main method is, and it also contains the classpath. I'd recommend that you read this.
You should also write your method using Process, like this example:
import java.io.*;
import java.util.*;
public class DoProcessBuilder {
public static void main(String args[]) throws IOException {
if (args.length <= 0) {
System.err.println("Need command to run");
System.exit(-1);
}
Process process = new ProcessBuilder(args).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
精彩评论