Java&多行批处理文件
我尝试在java中运行批处理文件,但文件执行不完全。代码如下:
Runtime.getRuntime().exec("cmd /c call "+path);
当我尝试在没有 java 的情况下手动运行它时,批处理工作正常。
批处理文件包含下一个代码:
cd C:\Downloads\
if not exist Documents mkdir Documents
move *.odt Documents
move *.doc Documents
if not exist Archives mkdir Archives
move *.tar Archives
move *.gz Archives
if not exist Music mkdir Music
move *.mp3 Music
Java 仅完成批处理到第五行。有人遇到过这个吗?
嗯,用batch,它又停止了此处。 Java 代码:链接。如果将 somename.ogg 添加到下载文件夹,脚本将转到下一行。所有文件夹(档案、文档等)都已存在,下载文件夹不包含 *.zip、*.tar、*.docx、*pdf 文件,但这些行顺利通过。为什么它正好停在ogg线上?
I try to run batch file in java, but file performed not completely. Code look like:
Runtime.getRuntime().exec("cmd /c call "+path);
When I try to run it manually, without java, batch works correct.
Batch file contains next code:
cd C:\Downloads\
if not exist Documents mkdir Documents
move *.odt Documents
move *.doc Documents
if not exist Archives mkdir Archives
move *.tar Archives
move *.gz Archives
if not exist Music mkdir Music
move *.mp3 Music
Java complete batch only to fifth line. Has anyone faced this?
Hm, with batch, it stoped again here. And Java code: link. If add to somename.ogg to downloads folder, script goes to next line. All folders (Archives, Documents etc) are already exists, and downloads folder doesn't contain *.zip, *.tar, *.docx, *pdf files, but these lines passed without problems. Why it stoped precisely at ogg-line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为问题是您的批处理脚本中存在错误。 MSDN 文档
if
命令说明了这一点:现在介绍一下您使用它的方式(
if not exit directory mkdir directory
)如果目录不存在,则可能会成功...但如果目录存在,则尝试再次创建该目录。哎呀...现在,您的 Java 应用程序读取并打印出该进程的错误流的内容,您很可能会看到一条错误消息,告诉您
Archives
目录已经存在。在我看来,忽略输出就是自找麻烦。I think the problem is that there is a bug in your batch script. The MSDN documentation for the
if
command states this:Now the way you are using it (
if not exist directory mkdir directory
) is likely to succeed if the directory doesn't exist ... but attempt to create the directory a second time if it does exist. Ooops ...Now, your Java application read and printed out the contents of the error stream for the process, you'd most likely see an error message telling you that the
Archives
directory already existed. Ignoring the output is asking for trouble, IMO.我敢打赌,您的 Java 程序中的
path
包含空格或类似的内容。您引用得正确吗?顺便说一句:Runtime.exec() 已被 ProcessBuilder 取代,它可以比 Runtime.exec() 更好地处理参数
试试这个:
I bet
path
in your Java program contains spaces or something similar. Are you quoting that properly?Btw: Runtime.exec() has been superseeded with ProcessBuilder which can handle arguments much better than Runtime.exec()
Try this:
天啊,伙计们!
我不明白为什么,但是:
不起作用,但是:
工作正常! %)
如果有人理解这一点,请解释一下。
OMG, GUYS!
I don't understand why, but:
doesn't works, BUT:
works correctly! %)
If anybody understands this, please, explain this.
如果您不小心处理 stdout 和 stderr 流,则很容易导致 ProcessBuilder 或 Runtime.exec() 死锁。文章
当 Runtime.exec() 不会< JavaWorld 上的 /code>
包含许多关于此问题和其他一些陷阱的有用信息。
如果可能的话,我建议使用 Commons Exec 因为所有必需的流泵送都已为您完成。
If you're not careful with your stdout and stderr streams it's very easy to deadlock ProcessBuilder or Runtime.exec(). The article
When Runtime.exec() won't
over at JavaWorld contains a lot of useful information on this and a couple of other pitfalls.If possible, I would suggest using Commons Exec because all the required stream pumping is done for you.