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在读取路径方面不是很智能,也可能是需要设置!

嗯,完美解决了,记录一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值