如何调试此错误?

时间:2014-08-12 16:37:33

标签: java batch-file

我有这个错误,我试图解决很长一段时间但无济于事。我想将java字符串传递给批处理文件。但是有错误。

btnStart.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent args)
        {
            String fileToPath = chooser.getSelectedFile().getAbsolutePath();
            try
            {   
                //create new process
                String command = "cmd /c start /wait "+DetectDrive+"\\imageinfo.bat";

                Process p = Runtime.getRuntime().exec(new String[]{command,"\""+fileToPath+"\""});

                //cause this process to stop until process p is terminated
                p.waitFor();
            } 
            catch (IOException | InterruptedException e1)
            {
                e1.printStackTrace();
            }
        }
    }

我想将String fileToPath传递给批处理文件以用于其他目的。例如,在我的批处理文件中:echo %1如果有效。但是我遇到了错误,我有很大的时间来解决它。

这是我的错误:

java.io.IOException: Cannot run program "cmd /c start /wait E:\\imageinfo.bat": 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 Volatility$3.actionPerformed(Volatility.java:187)
    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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
    ... 40 more

我不知道如何解决它。任何人都可以帮助我吗?我是java新手,但调试一直是我的弱点。任何帮助将不胜感激!!!

3 个答案:

答案 0 :(得分:2)

您正在将令牌合并为一个字符串(command),但仍将String[]传递给exec。这让exec感到困惑

String[] command = 
    new String[]{"cmd", "/c", "start", "/wait",
                 DetectDrive+"\\imageinfo.bat", fileToPath}; 
 Runtime.getRuntime().exec( command );

将fileToPath引用为exec。

稍后

您可以在单独的线程中执行子进程,以避免阻止您的应用程序。

答案 1 :(得分:0)

Runtime.exec(String)将整个字符串作为一个命令运行。你应该用 Runtime.exec(String[]),数组中的第一个字符串是命令,其他字符串是命令的参数。

答案 2 :(得分:0)

您可以使用ProcessBuilder将参数传递给外部进程,这非常简单。以下是ProcessBuilder的示例:

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();

其中param1和Param2是参数。您可以根据需要传递任意数量的参数。 希望这些能回答你的问题。