While trying to do automatic MYSQL backup using Java I keep getting the following error, and have been trying a work around all morning. The system is WindowsXP SP3, running Apache Tomcat 6, and Java 1.6.25
I keep getting this error:
ERROR com.busytech.barba.exception.BarbaException – java.io.IOException: Cannot run program “C:\Program”: CreateProcess error=2, The system cannot find the file specified
com.busytech.barba.exception.BarbaException: Cannot run program “C:\Program”: CreateProcess error=2, The system cannot find the file specified
at com.busytech.barba.persistence.BackUpAndRestoreDatabaseManager.getTableBackup(BackUpAndRestoreDatabaseManager.java:52)
at com.busytech.barba.servlet.checkcash.CashSummaryServlet.processRequest(CashSummaryServlet.java:95)
at com.busytech.barba.servlet.checkcash.CashSummaryServlet.doPost(CashSummaryServlet.java:495)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Unknown Source)
java.io.IOException: Cannot run program “C:\Program”: 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 com.busytech.barba.persistence.BackUpAndRestoreDatabaseManager.getTableBackup(BackUpAndRestoreDatabaseManager.java:30)
at com.busytech.barba.servlet.checkcash.CashSummaryServlet.processRequest(CashSummaryServlet.java:95)
at com.busytech.barba.servlet.checkcash.CashSummaryServlet.doPost(CashSummaryServlet.java:495)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.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.(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
… 21 more
347156 [http-8080-4] ERROR com.busytech.barba.exception.BarbaException – java.io.IOException: Cannot run program “C:\Program”: CreateProcess error=2, The system cannot find the file specified
com.busytech.barba.exception.BarbaException: Cannot run program “C:\Program”: CreateProcess error=2, The system cannot find the file specified
at com.busytech.barba.persistence.BackUpAndRestoreDatabaseManager.getTableBackup(BackUpAndRestoreDatabaseManager.java:52)
at com.busytech.barba.servlet.checkcash.CashSummaryServlet.processRequest(CashSummaryServlet.java:95)
at com.busytech.barba.servlet.checkcash.CashSummaryServlet.doPost(CashSummaryServlet.java:495)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Unknown Source)
This An Addition: this is what i have done try {
Process run = null;
if (System.getProperty("os.name").toLowerCase().contains("window")) {
run = Runtime.getRuntime().exec(SystemInformation.getWindowsMySQLDump() + " --host=" + host + " --port=" + port
+ " --user=" + user + " --password=" + password
+ " --compact --complete-insert --extended-insert "
+ "--skip-comments --skip-triggers --skip-lock-tables --single-transaction " + database + " " + table);
} else {
run = Runtime.getRuntime().exec("mysqldump --host=" + host + " --port=" + port
+ " --user=" + user + " --password=" + password
+ " --compact --complete-insert --extended-insert "
+ "--skip-comments --skip-triggers --skip-lock-tables --single-transaction " + database + " " + table);
}
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int count;
char[] cbuf = new char[BUFFER];
while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
temp.append(cbuf, 0, count);
}
br.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
Not sure that this is a MySQL or Backup question, but from the error:
Cannot run program “C:\Program”
It looks like you've forgotten to put quotes around a string somewhere.
If you try to run
c:\program files\myapp\something.exe
then it'll probably split at the first space, so you're trying to execute c:\program
- which I doubt is an actual executable. Try
"c:\program files\myapp\something.exe"
The problem is the path your application (in your case probably MySQL) is installed.
C:\Program Files\MySQL\bin\mysql.exe
The white space in "Program Files" causes the problem and you do not put quotes on it.
"C:\Program Files\MySQL\bin\mysql.exe"
You could use Apache Exec to start processes from Java. This handles all the common stuff that can go wrong when it comes to start processes from a Java application
If you could provide further information how you try to start the process.
Here is what I recommend (an implementation using Apache Commons Exec).
CommandLine cmdLine = CommandLine.parse("C:\\Program Files\\MySQL\\bin\\mysqldump");
cmdLine.addArgument("--user=");
// add further args here
Executor executor = new DefaultExecutor();
OutputStream stdOut = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdOut);
executor.setStreamHandler(psh);
try {
int exitCode = executor.execute(cmdLine);
String output = stdOut.toString();
BufferedReader lineReader = new BufferedReader(new StringReader(output));
String line;
while ((line = lineReader.readLine()) != null) {
log.debug("Reading line #{} - '{}'", lineCount, line);
}
} catch (IOException e) {
// handle the exception
}
This is the solution I used, it works like on windows and linux the above suggestion to use apache commons exec:
String cmdline = "mysqldump";
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
CommandLine commandLine = CommandLine.parse(cmdline);
commandLine.addArgument("--host=" + dbhost);
commandLine.addArgument("--port=" + dbport);
commandLine.addArgument("--user=" + dbuser);
commandLine.addArgument("--password=" + dbpass);
commandLine.addArgument(database);
Set keys = table.keySet();
for (Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = (String) table.get(key);
commandLine.addArgument(value);
}
commandLine.addArgument("-r");
//outputFile might be a file on the server that can be downloaded
//used apache file upload to download the files from the server
commandLine.addArgument(outputFile);
Executor executor = new DefaultExecutor();
executor.execute(commandLine, resultHandler);
resultHandler.waitFor();
And to restore the file to the server, Upload the file and here is the code to restore from a mysqldumpfile, this works if only mysql path has been set on the enviromental variables on windows but on linux it works as it is
String cmdline = "mysql";
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
CommandLine commandLine = CommandLine.parse(cmdline);
commandLine.addArgument("--user=" + dbuser);
commandLine.addArgument("--password=" + dbpass);
commandLine.addArgument("--force");
commandLine.addArgument("-v");
commandLine.addArgument("-e");
commandLine.addArgument("source");
commandLine.addArgument(backUpFile);
commandLine.addArgument(database);
Executor executor = new DefaultExecutor();
executor.execute(commandLine, resultHandler);
resultHandler.waitFor();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.