简体   繁体   English

Java中的运行时类

[英]Runtime class in java

How to execute a java program with the help of Runtime.getRuntime().exec(). 如何在Runtime.getRuntime()。exec()的帮助下执行Java程序。 For example we shall have the java file path as c:/java/abc.java. 例如,我们的Java文件路径为c:/java/abc.java。 Please help me with the code. 请帮助我的代码。

Assuming that abc.java contains a main method that you want to execute: 假设abc.java包含您要执行的主要方法:

Runtime.getRuntime().exec("javac c:\java\abc.java -d c:\java\")
Runtime.getRuntime().exec("java c:\java\abc")

Do not forget that: 不要忘记:

  • you may need to read stdout/stderr of a java program 您可能需要阅读Java程序的stdout / stderr
  • you may have to set/update environment variable and PATH before executing your java command 您可能必须在执行Java命令之前设置/更新环境变量和PATH

    CreateProcess: c:\\j2sdk1.4.0\\bin\\helloworld error=2 CreateProcess:c:\\ j2sdk1.4.0 \\ bin \\ helloworld错误= 2

means Win32's CreateProcess returns a 2 as error code when it cannot find the command you specify; 表示Win32的CreateProcess在找不到您指定的命令时返回2作为错误代码; more specifically, when the command does not refer to an executable file on its lookup path. 更具体地说,当命令在其查找路径上未引用可执行文件时。

Look at this SO question for 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 sdtin whatever command you want to execute. 此代码创建一个外壳程序(如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 cd command. 这种方法的兴趣在于重用Shell进程以从上一个命令中受益:先执行cd ,然后执行dir ,后一个命令将显示cd命令引用的目录的内容。

The same would be true for PATH settings, just before using javac or java . 在使用javacjava之前, PATH设置也是如此。

You should use ProcessBuilder instead of Runtime. 您应该使用ProcessBuilder而不是运行时。 Basic usage is like: 基本用法如下:

Process process = new ProcessBuilder(command).start();

You will find more code under the link above. 您可以在上面的链接下找到更多代码。 Also see this question . 也看到这个问题

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();

You mean you want a Java program to run another Java program. 您的意思是您希望一个Java程序运行另一个Java程序。 This SO thread might be helpful, in that case. 在这种情况下, 此SO线程可能会有所帮助。

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) {
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM