请教一个问题,谢谢大家了!在一个java程序里面用 Runtime.getRuntime().exce("java xxxx");
xxxx是一个只拥有main方法的类。
如何在调用Runtime.getRuntime().exce之后
得到xxxx的main的最后的System.exit(-1)的-1呢?
xxxx是一个只拥有main方法的类。
如何在调用Runtime.getRuntime().exce之后
得到xxxx的main的最后的System.exit(-1)的-1呢?
直接调用Runtime.getRuntime().exit(-1)
exit
public static void exit(int status)
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
This method calls the exit method in class Runtime. This method never returns normally. The call System.exit(n) is effectively equivalent to the call: Runtime.getRuntime().exit(n)
Parameters:
status - exit status.
Throws:
SecurityException - if a security manager exists and its checkExit method doesn't allow exit with the specified status.
See Also:
Runtime.exit(int)
public static void exit(int status)终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
该方法调用 Runtime 类中的 exit 方法。该方法永远不会正常返回。 调用 System.exit(n) 实际上等效于调用: Runtime.getRuntime().exit(n)
参数:
status - 退出状态。
抛出:
SecurityException - 如果安全管理器存在并且其 checkExit 方法不允许以指定状态退出。
另请参见:
Runtime.exit(int)
System.exit(-1) 这个函数无返回值啊!
linux里面的shell调用java之后
就能得到java里面的System.exit(ret)的ret
在java里面的ret不一定是0,1什么的
我调用之后得到ret
可能么?
p.waitFor();p.exitValue()就可以
但是要注意,如果你的xxxx类有输出控制台,那么不能直接调用p.waitFor(),应该调用p.getInputStream()返回流,一直读取这个流才行。否则在Windows上运行会导致xxxx停止不动的
一直都是0,不管我设定ret是什么,得到都是0.
就是正常运行完的意思吧?
前者代表正常退出,后者非正常退出
Process p = Runtime.getRuntime().exec("java -cp bin chdw.MyTest aa");
p.waitFor();
System.out.println(p.exitValue());
} else
System.exit(-2);
你能取到MyTest里面的人为设置的System.exit(-2)的-2??
public static void main(String[] args) throws Exception {
if(args.length == 0) {
Process p = Runtime.getRuntime().exec("java -cp bin chdw.MyTest aa");
p.waitFor();
System.out.println(p.exitValue());
} else
System.exit(-2);
}
}
这个程序就会打印-2,就是p.exitValue的值。
我是这么写的:
在第一个程序里面
public class Class1{
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("java MyTest");
p.waitFor();
System.out.println(p.exitValue());
}
}
被调用的程序 MyTest :
public class MyTest {
public static void main(String[] args) throws Exception {
System.exit(-2); // 直接结束
}
}
我是想在第一个程序里面得到第二个程序的exit值啊。
不好意思,看错了。。
已经弄好了。谢谢
我用的如果是 Runtime.getRuntime().exec("cmd /c start java MyTest aa")
这样做怎么也得不到MyTest的值
估计是因为我启动了cmd窗口
所以得到的永远是cmd窗口的返回值。
遗憾啊