如何在该目录上更改目录和运行命令?

时间:2016-10-10 01:04:01

标签: java cmd

示例:我想将目录更改为:C:/ temp / hacking / passsword 并执行如下命令:java Helloworld arg1 arg2 我怎么能用java做到这一点?

1 个答案:

答案 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();

还有其他一些类似于此问题的问题,我建议你也去看看它们以获得一个更好的主意。