示例:我想将目录更改为:C:/ temp / hacking / passsword 并执行如下命令:java Helloworld arg1 arg2 我怎么能用java做到这一点?
答案 0 :(得分:2)
试试这个:
Process pr = builder.start();
String[] commands = {"commands"};
ProcessBuilder builder = new ProcessBuilder(commands);
builder = builder.directory(new File(/one/two/dir));
pr = builder.start();
或者如果您更喜欢这种方法:
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"C:\\Users\\Test\" && dir");
Process pr = builder.start();
还有其他一些类似于此问题的问题,我建议你也去看看它们以获得一个更好的主意。