我遇到了这个错误,我尝试了很长时间但没有成功。我想将 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/