通过控制台制作文件时出现Java错误 [英] Java error in making file through console

查看:556
本文介绍了通过控制台制作文件时出现Java错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用此代码通过java中的cmd创建一个文件

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

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

我收到这个恼人的错误:

and i get this annoying error:

    Exception in thread "main" java.io.IOException: Cannot run program "mkdir": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at LFID.main(LFID.java:11)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

我不知道是什么导致它如此帮助。

I have no idea what's causing it so help.

顺便提一下,请不要告诉我如何创建不通过cmd的文件夹,我需要这样做。谢谢。

By the way please don't tell me how to create a folder not through cmd, I need to do it this way. Thanks.

推荐答案

mkdir 不是可以启动的独立可执行文件作为一个单独的进程 - 它是Windows命令shell可以理解的命令。

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

所以你可以运行 cmd.exe / c mkdir ...

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

或者:

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

...但我仍然建议只使用 File.mkdir 而不是......为什么在Java中可以调用外部进程? (如果你要指定一个奇怪的要求,它有助于给它一些更多的背景......)

... 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...)

这篇关于通过控制台制作文件时出现Java错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆