头痛 - 从 java 运行批处理文件
首先我要说的是,我已经浏览了所有 Q&AI 可以找到的内容,无论是网站内还是网站外,但我仍然遇到困难。
我的程序:
我的程序所做的就是在与我的程序相同的目录中运行批处理文件。
代码是:
try {
Process p = Runtime.getRuntime().exec("cmd /c start startclient.bat");
} catch (IOException ex) {
Logger.getLogger(MCPFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
当我执行代码时,我得到警告窗口:
Windows cannot find 'startclient.bat'. Make sure you typed the name correctly, and then try again.
如果我指定目录:
Process p = Runtime.getRuntime().exec("cmd /c start C:\\Folder\\startclient.bat");
我得到:
The system cannot find the path specified.
Press any key to continue . . .
C:\Windows\system32>
所以我未经教育的猜测是,当我通过 java 调用批处理文件时,它从“C:\Windows\”开始系统32>”但是当我双击批处理文件时,它从本地目录开始。
我该如何解决这个问题?
:(
PS 更重要的是,我去年确实让这个东西工作过,但由于某种原因它不再起作用了。
聚苯硫醚 我运行的是 Win 7,一切都是最新的。
So let me start by saying I've gone though every Q&A I can find, both on/off the site, and I'm still hitting a brick wall.
My Program:
All my program does is run a batch file in the same directory as my program.
The code is:
try {
Process p = Runtime.getRuntime().exec("cmd /c start startclient.bat");
} catch (IOException ex) {
Logger.getLogger(MCPFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
When I execute the code I get the warning window:
Windows cannot find 'startclient.bat'. Make sure you typed the name correctly, and then try again.
If I specify the directory with:
Process p = Runtime.getRuntime().exec("cmd /c start C:\\Folder\\startclient.bat");
I get:
The system cannot find the path specified.
Press any key to continue . . .
C:\Windows\system32>
So my uneducated guess is that when I'm calling the batch file through java it's starting in "C:\Windows\system32>" but when I just double click the batch file its starting from the local directory.
How do I fix this?
:(
PS
The kicker is, I actually had this thing working last year but for some reason it won't behave anymore.
PPS
I'm running Win 7 and everything is up to date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(我只是简单地发表评论,但我还没有足够的代表来评论,因此这个“答案”)
我使用过很多从 Java 调用的批处理文件(都在 Linux、OS X 上)和 Windows),并且首先要知道的是,您基本上永远不应该使用带有字符串的构造函数,因为它是有问题的。
您最好始终自己创建参数数组并使用此方法:
您还必须知道正确使用流可能很棘手。在许多情况下,您最好只使用可以更轻松地处理批处理文件的库。
例如,您可能喜欢 Apache 的 commons exec,而不是重新发明轮子:
http://commons.apache。组织/执行/
(I'd simply comment but I don't have enough rep yet to comment hence this "answer")
I've worked with a lot of batch files called from Java (both on Linux, OS X and Windows) and the first thing to know is that you should basically never ever use the constructor taking a string because it is, well, just problematic.
You're better to always create the array of arguments yourself and use this method:
You also have to know that correctly consuming the streams can be tricky. In many cases you're better to simply use libraries that make working with batch files easier.
For example, instead of re-inventing the wheel you may like Apache's commons exec here:
http://commons.apache.org/exec/
当我指定 C:\Folder\startclient.bat 等目录时,下面的反斜杠
应该适合您。嗯,我希望如此。对我有用。
When i specify the directory like C:\Folder\startclient.bat I have the back slashes after
Below should work for you. Well, i hope so. works for me.