从 Java 运行时执行批处理文件时,本机 DOS 命令无法运行

发布于 2024-08-13 10:14:08 字数 664 浏览 3 评论 0原文

当我直接在 DOS 中执行批处理文件时,一切都按预期运行。 但是,当我从 Java runTime 执行批处理文件时,它将仅运行调用 jar 文件(即调用 JVM)的命令。它运行任何本机 dos 命令。

一个问题是我没有控制台来知道为什么会发生这种情况。我想知道是否是权限问题,但我不知道。有人以前见过这个吗?

使用的 Java 代码如下所示:

Runtime.getRuntime().exec("c:\targetFolder\myBatch.bat"); //(为了简单起见,在此处进行了编辑。)


批处理文件看起来像这样(注意我已经简化了它):
myBatch.bat:

调用 java myJar.jar 等等 --- 是的
复制outputFile.out outputFile.bak --- 否
mkdir testDir --- 否
调用 java myJar.jar 等等 --- 是的
调用 someOther.bat --- 否

---yes 行运行良好,我看到了预期的结果
---no 行运行,但我不知道为什么不运行,因为没有控制台告诉我。

感谢您的帮助! 麦克风

When I execute the batch file directly in DOS, everything runs as expected.
But when I execute the batch file from Java runTime, it will run only the commands that invoke jar files (ie. invoke the JVM). It does not run any native dos commands.

One problem is that I have no console to know why this is happening. I'm wondering if it's a permissions problem, but I have no idea. Anyone out there see this before?

The Java code used looks something like this:

Runtime.getRuntime().exec("c:\targetFolder\myBatch.bat"); // (Edited here for simplicity.)

The batch file looks something like this (noting that I've simplified it):
myBatch.bat:

call java myJar.jar blah blah --- yes
copy outputFile.out outputFile.bak --- NO
mkdir testDir --- NO
call java myJar.jar blah blah --- yes
call someOther.bat --- NO

The ---yes lines run fine and I see the expected results
The ---no lines do not run, but I have no idea why not b/c there is no console to tell me.

Thanks for any help!!
Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风铃鹿 2024-08-20 10:14:08

您必须运行 Windows 命令处理器(shell),并将批处理文件作为参数提供给它。

Runtime.getRuntime().exec( "cmd.exe /C c:\\targetFolder\\myBatch.bat" );

You have to run the Windows command processor (the shell), giving it the batch file as an argument.

Runtime.getRuntime().exec( "cmd.exe /C c:\\targetFolder\\myBatch.bat" );
挽手叙旧 2024-08-20 10:14:08

第二个 java 调用执行的事实表明所有 NO 行仍在执行,但只是不显示任何输出。 打开回显

通过@ECHO ON

您是否尝试过在第一行中

?其次,你的问题可能是工作目录错误。像这样指定工作目录

Runtime.getRuntime().exec("c:\targetFolder\myBatch.bat",null,"c:\targetFolder"); 

The fact that the second java calls executes indicates that all your NO lines are still executing, but just not displaying any output. Have you tried turning on echo on via

@ECHO ON

in your first line?

Secondly, your problem is probably the wrong working directory. Specify the working directory like so

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