java - runtime。exec
来源:趣味经验馆 1.41W
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
java runtime.exec是什么?让我们一起来了解一下吧!
首先,java runtime.exec是java程序里一个很方便的方法,java可以通过运用Runtime.exec()在特定环境和工作目录的特定进程里执行外部程序或命令。
java runtime.exec有下面几种调用方法:
* public Process exec(String command); * public Process exec(String [] cmdArray); * public Process exec(String command, String [] envp); * public Process exec(String [] cmdArray, String [] envp);
实战演练,具体步骤如下:
* Java Runtime exec()方法*/import java.io.File;public class RuntimeDemo {undefinedpublic static void main(String[] args) {undefinedtry {undefined// print a messageSystem.out.println("Executing notepad.exe...");// create a file with the working directory we wishFile dir = new File("C:/");// create a process and execute notepad.exe and currect environmentProcess process = Runtime.getRuntime().exec("notepad.exe",null, dir);// print another messageSystem.out.println("Notepad should now open.");} catch (Exception ex) {undefinedex.printStackTrace();}}}