Java&多行批处理文件

发布于 2024-10-19 13:44:21 字数 814 浏览 10 评论 0原文

我尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-10-26 13:44:21

我认为问题是您的批处理脚本中存在错误。 MSDN 文档 if 命令说明了这一点:

您不能使用 if 命令直接测试目录,但空 (NUL) 设备确实存在于每个目录中。因此,您可以测试空设备以确定目录是否存在。以下示例测试目录是否存在:

如果存在 c:\mydir\nul 则转到进程

现在介绍一下您使用它的方式(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:

You cannot use the if command to test directly for a directory, but the null (NUL) device does exist in every directory. As a result, you can test for the null device to determine whether a directory exists. The following example tests for the existence of a directory:

if exist c:\mydir\nul goto process

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.

傾城如夢未必闌珊 2024-10-26 13:44:21

我敢打赌,您的 Java 程序中的 path 包含空格或类似的内容。您引用得正确吗?

顺便说一句:Runtime.exec() 已被 ProcessBuilder 取代,它可以比 Runtime.exec() 更好地处理参数

试试这个:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c",  "call \"" + path "\""); 
builder.start();

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:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c",  "call \"" + path "\""); 
builder.start();
烟酒忠诚 2024-10-26 13:44:21

天啊,伙计们!

我不明白为什么,但是:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\""); 
builder.start();

不起作用,但是:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\""); 
BufferedReader br = new BufferedReader(new InputStreamReader(builder.start().getInputStream()));
String line;
while ((line=br.readLine())!=null) {
    System.out.println(line);
}

工作正常! %)

如果有人理解这一点,请解释一下。

OMG, GUYS!

I don't understand why, but:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\""); 
builder.start();

doesn't works, BUT:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\""); 
BufferedReader br = new BufferedReader(new InputStreamReader(builder.start().getInputStream()));
String line;
while ((line=br.readLine())!=null) {
    System.out.println(line);
}

works correctly! %)

If anybody understands this, please, explain this.

海拔太高太耀眼 2024-10-26 13:44:21

如果您不小心处理 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文