从Java执行命令时出现问题

时间:2011-05-25 13:59:58

标签: java user-interface process action exec

我已经构建了这个actionPerformed方法,以便它读取我传递给按钮的字符串(我需要创建自己的按钮类来保存这个新字符串)并根据它所说的,它会执行不同的操作。字符串的一种可能性是:shell(“”)。这应该在后台运行系统命令(windows中的命令行,unix / linux中的shell命令)。这是该方法的来源:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.button) {
            if (password != "") {

            }
            if (action.startsWith("shell(\"")) {
                String tmpSHELL = action.substring(7, action.length() - 2);

                try {
                    Process p = Runtime.getRuntime().exec(tmpSHELL);
                } catch (IOException e1) {
                    ErrorDialog error = new ErrorDialog("Error handling your shell action");
                    System.exit(0);
                }

            }
            else if (action.startsWith("frame(\"")) {
                String tmpFRAME = action.substring(7, action.length() - 2);
                MenuFrame target = ConfigReader.getFrame(tmpFRAME);
                this.parent.setVisible(false);
                this.parent.validate();
                target.setVisible(true);
                target.validate();
            }
            else if (action.equals("exit()")) {
                System.exit(0);
            }
            else {
                ErrorDialog error = new ErrorDialog("You config file contains an invalid action command. Use either shell(), frame() or exit()");
                System.exit(0);
            }   
        }
    }

我知道我进入了该方法,但我不确定该命令是否正在成功执行。我目前处于Windows环境中,因此我制作了一个简单的批处理脚本,在打印C:驱动器的树之前回显一些文本然后等待击键。我将.bat放入我的工作java目录并传递字符串shell(“test”)(test是批处理文件的名称)。但是,当我单击按钮时,我得到一个错误对话框(我上面编码的那个)。

我的代码是否有问题,或者我对如何在java中执行shell命令的理解?该命令抛出IO异常,但我似乎无法弄清楚原因。在此先感谢您的帮助。

堆栈跟踪:

java.io.IOException: Cannot run program "test": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at Button.actionPerformed(Button.java:52)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 30 more

3 个答案:

答案 0 :(得分:1)

在Windows上尝试命令行:

"cmd test /c"

答案 1 :(得分:1)

这是因为在系统PATH环境变量中找不到命令test

如果转到命令行并键入test,则会失败。这是异常所表明的。

答案 2 :(得分:1)

The system cannot find the file specified

您的文件路径不正确。尝试传递绝对文件路径。

shell("C:/somedirectory/test.bat")

此外,您可以通过完全删除字符串测试来测试它。硬编码批处理文件的运行时执行,方法是使if语句始终为true并将批处理文件的路径传递给Runtime.getRuntime()。exec()

         if (password != "") {

         }
        if (true) {
            String tmpSHELL = action.substring(7, action.length() - 2);

            try {
                Process p = Runtime.getRuntime().exec("test");
            } catch (IOException e1) {
                ErrorDialog error = new ErrorDialog("Error handling your shell action");
                System.exit(0);
            }

        }

这应该产生相同的错误。然后用绝对文件路径替换文件路径,您应该能够执行批处理文件。

Process p = Runtime.getRuntime().exec("C:/somedirectory/test.bat");