如何使用 Runtime.getRuntime().exec 来 运行 任意 Java 程序?
How to use Runtime.getRuntime().exec to run an arbitrary Java program?
我发现以下代码使用 Runtime.getRuntime().exec
到 运行 任意程序(如 Notepad.exe )。
public class RuntimeDemo {
public static void main(String[] args) {
try {
// create a new array of 2 strings
String[] cmdArray = new String[2];
// first argument is the program we want to open
cmdArray[0] = "notepad.exe";
// second argument is a txt file we want to open with notepad
cmdArray[1] = "example.txt";
// print a message
System.out.println("Executing notepad.exe and opening example.txt");
// create a process and execute cmdArray and currect environment
Process process = Runtime.getRuntime().exec(cmdArray,null);
// print another message
System.out.println("example.txt should now open.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我从 Eclipse 运行 这个,它打开 Notepad.exe(或者我也可以使用 calc.exe)。
但是如果我们想从这个程序中 运行 一个任意的 Java 命令怎么办,比如:
java -version
-
这会运行吗?我在这里看到一个问题是.. 你在哪里看到它的结果?如果我是 运行ning 命令行,它会出现在命令行.. 但是这里?
- 使用
ProcessBuilder
of Runtime#exec
,它提供了一个更可配置的解决方案,并鼓励对命令和参数使用String[]
或List<String>
,解决了一堆问题,尤其是当您的参数包含空格时。
- 阅读
Process
的 InputStream
例如...
try {
String[] command = {"java.exe", "-?"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process exec = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
System.out.println("Process exited with " + exec.waitFor());
} catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}
输出
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Process exited with 0
这假定 java.exe
在 PATH
环境中,否则您可能需要提供它的完整路径(或更改工作目录)
我发现以下代码使用 Runtime.getRuntime().exec
到 运行 任意程序(如 Notepad.exe )。
public class RuntimeDemo {
public static void main(String[] args) {
try {
// create a new array of 2 strings
String[] cmdArray = new String[2];
// first argument is the program we want to open
cmdArray[0] = "notepad.exe";
// second argument is a txt file we want to open with notepad
cmdArray[1] = "example.txt";
// print a message
System.out.println("Executing notepad.exe and opening example.txt");
// create a process and execute cmdArray and currect environment
Process process = Runtime.getRuntime().exec(cmdArray,null);
// print another message
System.out.println("example.txt should now open.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我从 Eclipse 运行 这个,它打开 Notepad.exe(或者我也可以使用 calc.exe)。
但是如果我们想从这个程序中 运行 一个任意的 Java 命令怎么办,比如:
java -version
-
这会运行吗?我在这里看到一个问题是.. 你在哪里看到它的结果?如果我是 运行ning 命令行,它会出现在命令行.. 但是这里?
- 使用
ProcessBuilder
ofRuntime#exec
,它提供了一个更可配置的解决方案,并鼓励对命令和参数使用String[]
或List<String>
,解决了一堆问题,尤其是当您的参数包含空格时。 - 阅读
Process
的InputStream
例如...
try {
String[] command = {"java.exe", "-?"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process exec = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
System.out.println("Process exited with " + exec.waitFor());
} catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}
输出
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Process exited with 0
这假定 java.exe
在 PATH
环境中,否则您可能需要提供它的完整路径(或更改工作目录)