java java中的运行时类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/506154/
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
Runtime class in java
提问by Arun
How to execute a java program with the help of Runtime.getRuntime().exec(). For example we shall have the java file path as c:/java/abc.java. Please help me with the code.
如何在 Runtime.getRuntime().exec() 的帮助下执行 java 程序。例如,我们将 java 文件路径设为 c:/java/abc.java。请帮我写代码。
回答by Chris Bunch
Assuming that abc.java contains a main method that you want to execute:
假设 abc.java 包含您要执行的 main 方法:
Runtime.getRuntime().exec("javac c:\java\abc.java -d c:\java\")
Runtime.getRuntime().exec("java c:\java\abc")
回答by VonC
Do not forget that:
不要忘记:
- you may need to read stdout/stderr of a java program
you may have to set/update environment variable and PATH before executing your java command
CreateProcess: c:\j2sdk1.4.0\bin\helloworld error=2
- 您可能需要阅读 java 程序的 stdout/stderr
在执行 java 命令之前,您可能需要设置/更新环境变量和 PATH
CreateProcess: c:\j2sdk1.4.0\bin\helloworld error=2
means Win32's CreateProcessreturns a 2 as error code when it cannot find the command you specify; more specifically, when the command does not refer to an executable file on its lookup path.
表示 Win32CreateProcess在找不到您指定的命令时返回 2 作为错误代码;更具体地说,当该命令未在其查找路径上引用可执行文件时。
Look at this SO questionfor a more complete "Runtime.getRuntime().exec()" code, and also to this snippet.
查看此SO 问题以获得更完整的“ Runtime.getRuntime().exec()”代码,以及此代码段。
This code creates a shell (as in Runtime.getRuntime().exec("cmd /K")), in which you write on sdtinwhatever command you want to execute.
此代码创建了一个 shell(如 中所示Runtime.getRuntime().exec("cmd /K")),您可以在其中编写sdtin要执行的任何命令。
The interest of this approach is to reuse the shell process to benefit from a previous command: it you execute a 'cd', then execute a 'dir', the latter command would display the content of the directory referenced by the cdcommand.
这种方法的好处是重用 shell 进程以从前一个命令中受益:如果你执行一个 ' cd',然后执行一个 ' dir',后一个命令将显示该cd命令引用的目录的内容。
The same would be true for PATHsettings, just before using javacor java.
PATH设置也是如此,就在使用javacor之前java。
回答by Fabian Steeg
You should use ProcessBuilderinstead of Runtime. Basic usage is like:
您应该使用ProcessBuilder而不是 Runtime。基本用法如下:
Process process = new ProcessBuilder(command).start();
You will find more code under the link above. Also see this question.
您将在上面的链接下找到更多代码。另请参阅此问题。
回答by Naefy
String path1 = "f://" + File.separator+username+File.separator+progName;
Runtime runtime = Runtime.getRuntime();
String command = "javac -classpath " + path + " " + path1;
System.out.println(command);
Process process = runtime.exec(command);
InputStream error = process.getErrorStream();
回答by Adeel Ansari
You mean you want a Java program to run another Java program. This SO threadmight be helpful, in that case.
你的意思是你想让一个 Java 程序运行另一个 Java 程序。在这种情况下,此 SO 线程可能会有所帮助。
回答by Thorbj?rn Ravn Andersen
Please see the excellent resource which used to be called javaalmanac.
请参阅以前称为 javaalmanac 的优秀资源。
http://www.exampledepot.com/egs/java.lang/Exec.html
http://www.exampledepot.com/egs/java.lang/Exec.html
try {
// Execute a command with an argument that contains a space
String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
commands = new String[]{"grep", "hello world", "c:\Documents and Settings\f.txt"};
Process child = Runtime.getRuntime().exec(commands);
} catch (IOException e) {
}

