从Java执行命令时出现问题

如何解决从Java执行命令时出现问题

| 我已经构建了这个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是批处理文件的名称)。但是,当我单击按钮时,会出现一个错误对话框(上面我编码的对话框)。 我的代码是否有问题,或者我对执行Shell命令在Java中的工作方式有什么了解?该命令引发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
    

解决方法

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\");
    ,在Windows上,尝试命令行:
\"cmd test /c\"
    ,这是因为在系统PATH环境变量中找不到命令“ 7”。 如果转到命令行并键入“ 7”,它将失败。这就是异常所指示的内容。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?