简体   繁体   English

如何调试此错误?

[英]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. 我想将Java字符串传递给批处理文件。 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. 我想将String fileToPath传递给批处理文件以用于其他目的。 For example in my batch file: echo %1 if it works. 例如在我的批处理文件中:如果有效,请echo %1 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. 我是Java的新手,但是调试一直是我的弱点。 Any help will be greatly appreciated!!! 任何帮助将不胜感激!!!

You are combining tokens into one String ( command ) but still passing a String[] to exec . 您正在将令牌组合成一个String( command ),但仍将String[]传递给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. 将fileToPath的引用保留为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. Runtime.exec(String)将整个字符串作为一个命令运行。 You should use Runtime.exec(String[]) with the first string in the array being the command and the others the parameters for the command. 您应该使用Runtime.exec(String[]) ,数组中的第一个字符串是命令,其他则是命令的参数。

You can use ProcessBuilder for passing parameter to a external process which is very easy.Following is the example for ProcessBuilder: 您可以使用ProcessBuilder将参数传递到外部过程,这非常容易。以下是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. 其中param1和Param2是参数。您可以根据需要传递任意数量的参数。 Hope these answer your question. 希望这些回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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