java - 如何调试这个错误?

标签 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新手,但调试一直是我的弱点。任何帮助将不胜感激!!!

最佳答案

您将 token 组合成一个字符串(command),但仍将 String[] 传递给 exec。这让 exec 感到困惑

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

保留对 fileToPath 的引用以执行。

稍后

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

关于java - 如何调试这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25269610/

相关文章:

windows - 为什么控制台输出会阻止自引用批处理脚本干净地退出?

java - Box2D LibGDX 绳索问题

java - 关于适配器模式应用于 Java 中 Swing 事件的一些说明

java - 返回按字母顺序排序的动物列表,其最后一个字母不以参数中列出的任何字母结尾

Java JSON 反序列化错误

powershell - BAT/POWERSHELL副本(仅)昨天的文件

batch-file - Windows Bat文件可选参数解析

command-line - 有什么方法可以自动将命令历史记录保存到cmd.exe中的文件中,类似于bash的bash_history?

batch-file - 用于监视 FTP 服务器上更改的批处理脚本

java - 在 Java webapp 中使用 Markdown 需要什么?