gpt4 book ai didi

java - 从 Java 执行命令时出现问题

转载 作者:行者123 更新时间:2023-12-02 00:41:56 24 4
gpt4 key购买 nike

我构建了这个 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 是批处理文件的名称)。但是,当我单击该按钮时,我会收到一个错误对话框(我在上面编码的那个)。

我的代码有问题吗,或者我对在 java 中执行 shell 命令如何工作的理解有问题吗?该命令引发 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");

关于java - 从 Java 执行命令时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com