简体   繁体   中英

How to debug this error?

I have this error which I have tried to solve for a long time but to no avail. I wanted to pass a java string to a batch file. But there's error.

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();
            }
        }
    }

I wanted to pass the String fileToPath to a batch file for some other purposes. For example in my batch file: echo %1 if it works. But I got errors which I have a big time solving it.

Here's my error:

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

I have no idea how to solve it. Can anyone help me with it? I'm new to java but debugging has always been my weakness. Any help will be greatly appreciated!!!

You are combining tokens into one String ( command ) but still passing a String[] to exec . This confuses exec

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

Leave quoting of the fileToPath to exec.

Later

You can execute the subprocess in a separate Thread to avoid blocking your application.

Runtime.exec(String) will run the whole string as one command. You should use Runtime.exec(String[]) with the first string in the array being the command and the others the parameters for the command.

You can use ProcessBuilder for passing parameter to a external process which is very easy.Following is the example for ProcessBuilder:

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

Where param1 and Param2 are parameters.You can pass as many parameter as you want. Hope these answer your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM