Java:运行cmd命令(一次有多个参数的多个命令)
我运行cmd(命令行)并以这种方式从Java运行我的批处理文件:
final String cmd = "cmd /c C: && dir && cd C:\MyApp\Maxi && dir && C:\MayApp\Maxi\deploy.bat"; try { Process process = Runtime.getRuntime().exec(cmd); final InputStream in = process.getInputStream(); int ch; while((ch = in.read()) != -1) { System.out.print((char)ch); } } catch (IOException e) { System.out.println("IOException on CMD executing statement"); e.printStackTrace(); }
它运作成功,但我修改了批处理文件并添加了一些参数,所以我必须将一个名称传递给批处理文件,所以我尝试了这个:(我发送“Name1”作为参数)
final String cmd = "cmd /c C: && dir && cd C:\MyApp\Maxi && dir && C:\MayApp\Maxi\deploy.bat Name1"; try { Process process = Runtime.getRuntime().exec(cmd); final InputStream in = process.getInputStream(); int ch; while((ch = in.read()) != -1) { System.out.print((char)ch); } } catch (IOException e) { System.out.println("IOException on CMD executing statement"); e.printStackTrace(); }
但它现在不工作,命令没有执行。 我只获得“dir”命令输出。
有人可以帮忙吗?
注意:命令在CMD上成功运行,但它无法在java中运行。
为什么要在一个命令中执行多个任务? 例如,改为C:\,dir,然后执行?
您可以轻松地将所有这些任务归入一个批处理文件。
如果某些目录结构发生变化,这将帮助您“不”再次编译代码。