头痛 - 从 java 运行批处理文件

发布于 2024-12-09 03:00:50 字数 986 浏览 1 评论 0原文

首先我要说的是,我已经浏览了所有 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 技术交流群。

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

发布评论

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

评论(2

稚然 2024-12-16 03:00:50

(我只是简单地发表评论,但我还没有足够的代表来评论,因此这个“答案”)

我使用过很多从 Java 调用的批处理文件(都在 Linux、OS X 上)和 Windows),并且首先要知道的是,您基本上永远不应该使用带有字符串的构造函数,因为它是有问题的。

您最好始终自己创建参数数组并使用此方法:

public Process exec(String [] cmdArray)

您还必须知道正确使用流可能很棘手。在许多情况下,您最好只使用可以更轻松地处理批处理文件的库。

例如,您可能喜欢 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:

public Process exec(String [] cmdArray)

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/

感悟人生的甜 2024-12-16 03:00:50

当我指定 C:\Folder\startclient.bat 等目录时,下面的反斜杠

C:\\ as forward slashes and only one.

C:\\Folder/startclient.bat

应该适合您。嗯,我希望如此。对我有用。

    try {
        Runtime rt = Runtime.getRuntime();
        rt.exec("cmd.exe /c start C:\\Folder/startclient.bat");
    } catch (Exception ex){

    }

When i specify the directory like C:\Folder\startclient.bat I have the back slashes after

C:\\ as forward slashes and only one.

C:\\Folder/startclient.bat

Below should work for you. Well, i hope so. works for me.

    try {
        Runtime rt = Runtime.getRuntime();
        rt.exec("cmd.exe /c start C:\\Folder/startclient.bat");
    } catch (Exception ex){

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