得到System.exit(-1)的返回值

fdy1984 2010-06-18 10:29:12
请教一个问题,谢谢大家了!

在一个java程序里面用 Runtime.getRuntime().exce("java xxxx");
xxxx是一个只拥有main方法的类。
如何在调用Runtime.getRuntime().exce之后
得到xxxx的main的最后的System.exit(-1)的-1呢?
...全文
486 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdy1984 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chdw 的回复:]

我的可以非常正常的读取出来啊

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);
[/Quote]
我怎么取不到呢??
你能取到MyTest里面的人为设置的System.exit(-2)的-2??
fdy1984 2010-06-24
  • 打赏
  • 举报
回复
调用MyTest的时候
我用的如果是 Runtime.getRuntime().exec("cmd /c start java MyTest aa")
这样做怎么也得不到MyTest的值
估计是因为我启动了cmd窗口
所以得到的永远是cmd窗口的返回值。
遗憾啊

fdy1984 2010-06-24
  • 打赏
  • 举报
回复
chdw:
不好意思,看错了。。
已经弄好了。谢谢
fdy1984 2010-06-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 chdw 的回复:]

public class MyTest {
public static void main(String[] args) throws Exception {
if(args.length == 0) {
Process p = Runtime.getRuntime().exec("java -cp bin hdw.MyTest aa");
p.waitFor();
System.out.println(p.exitValue());
} else
System.exit(-2);
}
}
[/Quote]
你这个自己调用自己?
我是这么写的:
在第一个程序里面
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值啊。
ChDw 2010-06-24
  • 打赏
  • 举报
回复
非常正常啊,我不知道你代码怎么写的,我贴的代码就是MyTest.java啊

public class MyTest {
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的值。
macrotea-cn 2010-06-23
  • 打赏
  • 举报
回复
学习路过
qbd321 2010-06-23
  • 打赏
  • 举报
回复
可以使exit(0),也可以使exit(-1)
前者代表正常退出,后者非正常退出
fdy1984 2010-06-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chdw 的回复:]

是返回一个Process对象的,

Process p = Runtime.getRuntime().exec("java xxxx");
p.waitFor();

p.exitValue()就可以


但是要注意,如果你的xxxx类有输出控制台,那么不能直接调用p.waitFor(),应该调用p.getInputStream()返回流,一直读取这个流才行。否则在Windows……
[/Quote]
这个也得不到System.exit(ret)的ret啊。
一直都是0,不管我设定ret是什么,得到都是0.
就是正常运行完的意思吧?
qusic 2010-06-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fdy1984 的回复:]
引用 7 楼 chdw 的回复:

是返回一个Process对象的,

Process p = Runtime.getRuntime().exec("java xxxx");
p.waitFor();

p.exitValue()就可以


但是要注意,如果你的xxxx类有输出控制台,那么不能直接调用p.waitFor(),应该调用p.getInputStream()返回流,一……
[/Quote]
要等到被执行的程序退出了才能获得正确的值
pauliuyou 2010-06-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chdw 的回复:]

是返回一个Process对象的,

Process p = Runtime.getRuntime().exec("java xxxx");
p.waitFor();

p.exitValue()就可以


但是要注意,如果你的xxxx类有输出控制台,那么不能直接调用p.waitFor(),应该调用p.getInputStream()返回流,一直读取这个流才行。否则在Windows……
[/Quote]

正解
ChDw 2010-06-23
  • 打赏
  • 举报
回复
我的可以非常正常的读取出来啊

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);
ChDw 2010-06-18
  • 打赏
  • 举报
回复
是返回一个Process对象的,

Process p = Runtime.getRuntime().exec("java xxxx");
p.waitFor();

p.exitValue()就可以


但是要注意,如果你的xxxx类有输出控制台,那么不能直接调用p.waitFor(),应该调用p.getInputStream()返回流,一直读取这个流才行。否则在Windows上运行会导致xxxx停止不动的
  • 打赏
  • 举报
回复
不懂shell
dr_lou 2010-06-18
  • 打赏
  • 举报
回复
不懂shell
fdy1984 2010-06-18
  • 打赏
  • 举报
回复
java里面我没这么写过
linux里面的shell调用java之后
就能得到java里面的System.exit(ret)的ret
在java里面的ret不一定是0,1什么的
我调用之后得到ret
可能么?
dr_lou 2010-06-18
  • 打赏
  • 举报
回复
exit
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) 这个函数无返回值啊!
mehere8 2010-06-18
  • 打赏
  • 举报
回复
Runtime.getRuntime().exit(int) 与 System.exit(int) 一样
直接调用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)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧