I need to launch another Java program at runtime, this is my code
try {
String cmd2 = "java -jar c:\\test\\deploy\\framework_e_app.jar";
Process p = Runtime.getRuntime().exec(cmd2);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.print("<STDOUT>");
System.out.print(line);
System.out.println("</STDOUT>");
}
InputStream stderr = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line2 = null;
System.out.print("<STDERROR>");
while ( (line2 = br.readLine()) != null)
System.out.print(line2);
System.out.println("</STDERROR>");
} catch (IOException e) {
e.printStackTrace();
}
This is the only way i got it working, but it is annoying because it search configuration files in the current path.
I tried using this as cmd2:
String[] cmd2 = new String[4];
cmd2[0] = "cmd";
cmd2[1] = "/C";
cmd2[2] = "cd test\\deploy";
cmd2[3] = "java -jar framework_e_app.jar";
I couldn’t get working the directory change. What’s the right syntax? (Im in a Windows environment, obviously).
the cd does not stick. each command is executed in a different process. use a form of exec that has the working directory like http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String%5B%5D,%20java.lang.String%5B%5D,%20java.io.File%29