尝试使用 apache commons-exec jar 打开 cmd 时出错
error trying to open cmd using apache commons-exec jar
我正在尝试从 apache commons-exec 执行 mysql 转储,但出现以下错误
Exception in thread "main" java.io.IOException: Cannot run program
"cmd.exe \c" (in directory "."): CreateProcess error=2, The system
cannot find the file specified at
java.lang.ProcessBuilder.start(ProcessBuilder.java:470) at
java.lang.Runtime.exec(Runtime.java:593) at
org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61)
at
org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279)
at
org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336)
at
org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at
org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
at
com.etq.e2mc.platform.windows.WindowsProcess.execCommons(WindowsProcess.java:87)
at
com.etq.e2mc.platform.windows.WindowsProcess.main(WindowsProcess.java:79)
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.(ProcessImpl.java:177) at
java.lang.ProcessImpl.start(ProcessImpl.java:28) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:452) ... 8 more
这是我正在使用的代码,它非常简单直接,但我不明白为什么它不调用 cmd(注意:尝试在没有 cmd 的情况下直接调用 mysql dump 和我得到相同类型的错误),将不胜感激任何帮助
public static void main(String[] args) throws Exception {
execCommons();
}
public static void execCommons() throws ExecuteException, IOException {
CommandLine cmd = new CommandLine("cmd.exe /c");
cmd.addArguments("mysqldump");
cmd.addArguments(new String[] { "-u", "root", " -P", "3316", " -h", "localhost", " -A", ">"});
cmd.addArguments("\"G:\test.sql \"" , false);
new DefaultExecutor().execute(cmd);
}
从(普通)java 执行外部程序,在 windows:
Process p = Runtime.getRuntime().exec("cmd /c start /wait \"title\" \""+exe_path_and_other_parameters+"\"");
p.waitFor();
出于某种原因,commons-exec 似乎不喜欢问题中的命令措辞(用 "cmd.exe /c"
初始化 CommandLine
),重新措辞后一切似乎都正常它到以下
public static void main(String[] args) throws Exception {
execCommons();
}
public static void execCommons() throws ExecuteException, IOException {
CommandLine cmd = new CommandLine("cmd.exe ");
cmd.addArgument("/c");
String command = "mysqldump " + "-u" + "root" + " -P" + "3316" + " -h" + "localhost" + " -A >" + "\"G:\test.sql \"";
cmd.addArgument(command,false);
new DefaultExecutor().execute(cmd);
}
我不知道为什么会这样工作,因为文档中没有说明,但我把它留在这里以防它对某人有帮助。但如果有人有任何想法请告诉
我正在尝试从 apache commons-exec 执行 mysql 转储,但出现以下错误
Exception in thread "main" java.io.IOException: Cannot run program "cmd.exe \c" (in directory "."): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:470) at java.lang.Runtime.exec(Runtime.java:593) at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61) at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279) at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153) at com.etq.e2mc.platform.windows.WindowsProcess.execCommons(WindowsProcess.java:87) at com.etq.e2mc.platform.windows.WindowsProcess.main(WindowsProcess.java:79) 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.(ProcessImpl.java:177) at java.lang.ProcessImpl.start(ProcessImpl.java:28) at java.lang.ProcessBuilder.start(ProcessBuilder.java:452) ... 8 more
这是我正在使用的代码,它非常简单直接,但我不明白为什么它不调用 cmd(注意:尝试在没有 cmd 的情况下直接调用 mysql dump 和我得到相同类型的错误),将不胜感激任何帮助
public static void main(String[] args) throws Exception {
execCommons();
}
public static void execCommons() throws ExecuteException, IOException {
CommandLine cmd = new CommandLine("cmd.exe /c");
cmd.addArguments("mysqldump");
cmd.addArguments(new String[] { "-u", "root", " -P", "3316", " -h", "localhost", " -A", ">"});
cmd.addArguments("\"G:\test.sql \"" , false);
new DefaultExecutor().execute(cmd);
}
从(普通)java 执行外部程序,在 windows:
Process p = Runtime.getRuntime().exec("cmd /c start /wait \"title\" \""+exe_path_and_other_parameters+"\"");
p.waitFor();
出于某种原因,commons-exec 似乎不喜欢问题中的命令措辞(用 "cmd.exe /c"
初始化 CommandLine
),重新措辞后一切似乎都正常它到以下
public static void main(String[] args) throws Exception {
execCommons();
}
public static void execCommons() throws ExecuteException, IOException {
CommandLine cmd = new CommandLine("cmd.exe ");
cmd.addArgument("/c");
String command = "mysqldump " + "-u" + "root" + " -P" + "3316" + " -h" + "localhost" + " -A >" + "\"G:\test.sql \"";
cmd.addArgument(command,false);
new DefaultExecutor().execute(cmd);
}
我不知道为什么会这样工作,因为文档中没有说明,但我把它留在这里以防它对某人有帮助。但如果有人有任何想法请告诉