我需要在运行时启动另一个Java程序,这是我的代码
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();
}
这是我让它工作的唯一方法,但它很烦人,因为它在当前路径中搜索配置文件。
我尝试使用它作为 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";
我无法进行目录更改。正确的语法是什么? (显然,我在 Windows 环境中)。
最佳答案
CD 不粘。每个命令都在不同的进程中执行。使用具有工作目录的 exec 形式,例如 http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String[],%20java。 lang.String[],%20java.io.File%29
关于java - 运行时Exec改变路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256016/