问题描述
|
我已经构建了这个actionPerformed方法,以便它读取传递给按钮的字符串(我需要创建自己的按钮类来容纳此新字符串),并且根据其内容,它会执行不同的操作。字符串的一种可能是类似于:shell(\“ \”)。应该在后台运行系统命令(Windows中的命令行,unix / linux中的shell命令)。这是方法的来源:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.button) {
if (password != \"\") {
}
if (action.startsWith(\"shell(\\\"\")) {
String tmpSHELL = action.substring(7,action.length() - 2);
try {
Process p = Runtime.getRuntime().exec(tmpSHELL);
} catch (IOException e1) {
ErrorDialog error = new ErrorDialog(\"Error handling your shell action\");
System.exit(0);
}
}
else if (action.startsWith(\"frame(\\\"\")) {
String tmpFRAME = action.substring(7,action.length() - 2);
MenuFrame target = ConfigReader.getFrame(tmpFRAME);
this.parent.setVisible(false);
this.parent.validate();
target.setVisible(true);
target.validate();
}
else if (action.equals(\"exit()\")) {
System.exit(0);
}
else {
ErrorDialog error = new ErrorDialog(\"You config file contains an invalid action command. Use either shell(),frame() or exit()\");
System.exit(0);
}
}
}
我知道我已经进入方法,但是我不确定命令是否成功执行。我目前在Windows环境中,因此我制作了一个简单的批处理脚本,该脚本回显一些文本,然后等待击键,然后再打印C:驱动器的树。我将.bat放入我的工作Java目录中,并传递了字符串shell(\“ test \”)(test是批处理文件的名称)。但是,当我单击按钮时,会出现一个错误对话框(上面我编码的对话框)。
我的代码是否有问题,或者我对执行Shell命令在Java中的工作方式有什么了解?该命令引发IO异常,但我似乎无法弄清楚原因。在此先感谢您的帮助。
堆栈跟踪:
java.io.IOException: Cannot run program \"test\": 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 java.lang.Runtime.exec(Unknown Source)
at Button.actionPerformed(Button.java:52)
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.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)
... 30 more
解决方法
The system cannot find the file specified
您的文件路径不正确。尝试传递绝对文件路径。
shell(\"C:/somedirectory/test.bat\")
另外,您可以通过完全删除字符串测试来对此进行测试。通过使if语句始终为true并将批处理文件的路径传递给Runtime.getRuntime()。exec(),对批处理文件的运行时执行进行硬编码
if (password != \"\") {
}
if (true) {
String tmpSHELL = action.substring(7,action.length() - 2);
try {
Process p = Runtime.getRuntime().exec(\"test\");
} catch (IOException e1) {
ErrorDialog error = new ErrorDialog(\"Error handling your shell action\");
System.exit(0);
}
}
这应该产生相同的错误。然后用绝对文件路径替换文件路径,您应该能够执行批处理文件。
Process p = Runtime.getRuntime().exec(\"C:/somedirectory/test.bat\");
,在Windows上,尝试命令行:
\"cmd test /c\"
,这是因为在系统PATH环境变量中找不到命令“ 7”。
如果转到命令行并键入“ 7”,它将失败。这就是异常所指示的内容。