java編程思想_在Eclipse中運行process不能執行問題

在敲java編程思想18.9遠程控制時有如下情況

書上的例子one:

public class OSExcute {
	public static void command(String command){
		boolean err = false;
		try {
			String[] commands = command.split(" ");
			System.out.println(Arrays.toString(commands));
			Process process = new ProcessBuilder((commands)).start();
			BufferedReader results = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String s;
			while((s = results.readLine()) != null)
				System.out.println(s);
			BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			//report errors and return nonzero value to calling process if there are problems
			while((s = errors.readLine()) != null){
				System.err.println(s);
				err = true;
			}
		} catch (Exception e) {
			//Compensate for Windows 2000,which throws an exception for the default command line:
			if(command.startsWith("CMD /C"))
				command("CMD /C" + command);
			else
				throw new RuntimeException(e);
		};
		if(err)
			throw new OSExcuteException("Errors excuting " + command);
	}
}

書上的例子two:

public class OSExcuteDemo {
	public static void main(String[] args) {
		
		OSExcute.command("javap OSExcuteDemo");
	}
}	

ok,運行一下,報錯:

在這裏插入圖片描述

剛開始以爲亂碼問題,設置了下process.getInputStream()和process.getErrorStream()的編碼,然並沒有什麼用

cmd中運行javap helloWord沒問題,於是想到可能是路徑的問題,多方面嘗試,最後如下解決:

public class OSExcuteDemo {
	public static void main(String[] args) {
		
		OSExcute.command("javap F:\\think_in_java_git\\java_io_system\\bin\\examples\\OSExcuteDemo.class");
	}
}	

絕對路徑後連.class都加上了,好了,感覺eclipse在讀取路徑方面不是很智能,也可能是需要設置!

嗯,完美解決了,記錄一下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章