java Runtime.getRuntime().exec(),执行Java类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4358199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:02:08  来源:igfitidea点击:

Runtime.getRuntime().exec(), executing Java class

javaruntime.exec

提问by Little Jeans

I am executing Java class from inside my application.

我正在从我的应用程序内部执行 Java 类。

proc = Runtime.getRuntime().exec("java Test");

How can I recognize whether Testexecuted successfully or not (i.e. no exceptions)?

如何识别是否Test执行成功(即没有异常)?



Redirecting output / error:

重定向输出/错误:

proc = Runtime.getRuntime().exec(new String[] {
    "java",
    mclass,
    ">NUL 2>test.txt"
});

From cmd:

来自cmd

java Main >NUL 2>test.txt

回答by khachik

process.waitFor();
int exitCode = process.exitValue();
if(exitCode == 0) { // success }
else { // failed }

This works, if the Testis designed properly and returns appropriate exit codes (generally, >0 if something went wrong).

如果Test设计正确并返回适当的退出代码(通常,如果出现问题,则 >0)。

If you want to get Tests output/error message to determine what was wrong, you should get proc.getInputStream()(this returns the output stream of the child process), proc.getErrorStream()and read from the input streams in separated threads.

如果您想获取Tests 输出/错误消息来确定出了什么问题,您应该获取proc.getInputStream()(这将返回子进程的输出流),proc.getErrorStream()并在分离的线程中从输入流中读取。

Note that the child process will get blocked if it writes to error/output stream and there are no readers. So reading error/output streams of the process is useful in any cases.

请注意,如果子进程写入错误/输出流并且没有读取器,它将被阻止。因此,读取过程的错误/输出流在任何情况下都是有用的。

Another option to avoid child blocking is to redirect its error/output to a file and/or to /dev/null('NUL' for windows):

避免子阻塞的另一个选择是将其错误/输出重定向到文件和/或/dev/null(对于 Windows 为“NUL”):

Runtime.exec("java Test >/dev/null 2>&1");
Runtime.exec("java Test >/dev/null 2>erroroutput");

回答by user85421

Redirection is done by the shell processor, not by Runtime.exec()(at least not on Windows).
You need to execute your command by cmd.exe:

重定向是由外壳处理器完成的,而不是由Runtime.exec()(至少不是在 Windows 上)。
您需要通过以下方式执行您的命令cmd.exe

String command = "cmd /c java -classpath D:\dev\temp\ Main >NUL 2>test.txt";
proc = Runtime.getRuntime().exec(command);

回答by Marc

See the Process class

查看进程类

You can call proc.waitFor()to return an integer value. But you have to make sure that all output of the program is handled correctly (e.g. use the proc.getInputStream()method).

您可以调用proc.waitFor()以返回整数值。但是您必须确保正确处理程序的所有输出(例如使用该proc.getInputStream()方法)。

回答by Filip Spiridonov

Have you tried proc.exitValue()?

你试过proc.exitValue()吗?

回答by thejh

Errr... what? Do it this way:

呃……什么?这样做:

int response = (int)(getClass().getClassLoader().findClass("Test").
        getMethod("someStaticMethod", new String[]{}).
        invoke(null, new Object[]{}));

This invokes the static method "int someStaticMethod()" of the class "Test" which gets loaded dynamically.

这将调用动态加载的类“Test”的静态方法“int someStaticMethod()”。

回答by Rajat Bhadauria

You may use ProcessBuilder class of java to execute these files.

您可以使用 Java 的 ProcessBuilder 类来执行这些文件。