繁体   English   中英

如何调试此错误?

[英]How to debug this error?

我有这个错误,我已经尝试解决了很长一段时间,但无济于事。 我想将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的新手,但是调试一直是我的弱点。 任何帮助将不胜感激!!!

您正在将令牌组合成一个String( command ),但仍将String[]传递给exec 这使执行人员感到困惑

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

将fileToPath的引用保留为exec。

后来

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

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

您可以使用ProcessBuilder将参数传递到外部过程,这非常容易。以下是ProcessBuilder的示例:

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

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

暂无
暂无

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

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