在 Java 中启动进程? [英] Starting a process in Java?
本文介绍了在 Java 中启动进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 Java 中启动进程?在 .Net 中,这是通过例如:
Is there a way to start a process in Java? in .Net this is done with for example:
System.Diagnostics.Process.Start("processname");
Java 中是否有等价物,以便我可以让用户找到应用程序,然后它可以在任何操作系统上运行?
Is there an equivalent in Java so I can then let the user find the application and then it would work for any OS?
推荐答案
http://www.rgagnon.com/javadetails/java-0014.html
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Paths;
public class CmdExec {
public static void main(String args[]) {
try {
// enter code here
Process p = Runtime.getRuntime().exec(
Paths.get(System.getenv("windir"), "system32", "tree.com /A").toString()
);
// enter code here
try(BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
}
} catch (Exception err) {
err.printStackTrace();
}
}
}
您可以使用系统属性或类似方法获取本地路径.
You can get the local path using System properties or a similar approach.
http://download.oracle.com/javase/tutorial/必要的/环境/sysprop.html
这篇关于在 Java 中启动进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文