从 Java 运行时执行批处理文件时,本机 DOS 命令无法运行
当我直接在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须运行 Windows 命令处理器(shell),并将批处理文件作为参数提供给它。
You have to run the Windows command processor (the shell), giving it the batch file as an argument.
第二个 java 调用执行的事实表明所有 NO 行仍在执行,但只是不显示任何输出。 打开回显
通过@ECHO ON
您是否尝试过在第一行中
?其次,你的问题可能是工作目录错误。像这样指定工作目录
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