Java error in making file through console

后端 未结 1 1772
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 21:31

I want to make a file though the cmd in java using this code

    Runtime.getRuntime().exec(\"mkdir C:\\\\Users\\\\Nick\\\\test\");

and i ge

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 21:56

    mkdir isn't a standalone executable you can launch as a separate process - it's a command that the Windows command shell understands.

    So you could run cmd.exe /c mkdir ...:

    Runtime.getRuntime().exec("cmd.exe /c mkdir c:\\Users\\Nick\\test");
    

    Or:

    Runtime.getRuntime().exec(
        new String[] { "cmd.exe", "/c" "mkdir" "c:\\Users\\Nick\\test"});
    

    ... but I'd still recommend just using File.mkdir instead... why call out to an external process when you can do it within Java? (If you're going to specify an odd requirement, it helps to give some more context on it...)

    0 讨论(0)
提交回复
热议问题