全屏
java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)方法執行在指定環境和工作目錄的獨立進程中指定的命令和參數。字符串給定一個數組cmdarray,代表一個命令行標記和一個字符串數組envp,代表“環境”變量設置,此方法創建要在其中執行指定的命令新的進程。
啟動操作系統的過程是高度依賴于系統的。在眾多的事情都可能出錯是:未找到操作系統程序文件。
訪問該程序文件被拒絕。
工作目錄不存在。
java bufferedwriter,在這種情況下,一個異常將被拋出。異常的確切性質取決于系統,但它永遠是IOException異常的子類。
聲明
以下是java.lang.Runtime.exec()方法的聲明public?Process?exec(String[]?cmdarray,?String[]?envp,?File?dir)
參數cmdarray?-- 調用及其參數包含命令數組。envp?-- 字符串數組,其中的每個元素都有其格式為name = value設置環境變量,則返回null,如果子進程應該繼承當前進程的環境。
dir?-- 子進程的工作目錄,或null,如果子進程應該繼承當前進程的工作目錄。
返回值
java.lang.integer cannot be,該方法返回一個新的Process對象,用于管理子進程
異常SecurityException?-- 如果安全管理器存在,并且其checkExec方法不允許創建子進程
IOException?-- 如果發生I/ O錯誤
NullPointerException?--如果命令為空
IndexOutOfBoundsException?--?如果cmdarray是一個空數組(長度為0)
例子
import java.util.Scanner、此示例要求名為c:/test.txt在/文件夾C:/ folder :包含以下內容:Hello
下面的例子顯示lang.Runtime.exec()方法的使用。package?cn.sxt;
import?java.io.File;
public?class?RuntimeDemo?{
public?static?void?main(String[]?args)?{
try?{
java string contains,//?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]?=?"test.txt";
java reached end of file,//?print?a?message
System.out.println("Executing?notepad.exe?and?opening?test.txt");
//?create?a?file?which?contains?the?directory?of?the?file?needed
File?dir?=?new?File("c:/");
//?create?a?process?and?execute?cmdArray?and?currect?environment
Process?process?=?Runtime.getRuntime().exec(cmdArray,?null,?dir);
java.lang.string cannot be cast?//?print?another?message
System.out.println("test.txt?should?now?open.");
}?catch?(Exception?ex)?{
ex.printStackTrace();
}
}
java.lang.OutOfMemoryError。}
讓我們來編譯和運行上面的程序,這將產生以下結果:Executing?notepad.exe?and?opening?test.txt
test.txt?should?now?open.
分享到:
0評論
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态