好吧,我的Java应用程序不能使用windows运行时环境执行mysqldump备份。我有用于捕获异常的打印输出代码,并且我没有看到异常被抛出,当备份代码被执行时,在表面上看起来很好。
我还在命令行控制台中测试了mysqldump命令;在那里它可以正常工作。
代码:
Runtime rt = Runtime.getRuntime();
try {
Process pr = rt.exec("mysqldump -u test --password=pass lager > newBackup.sql");
} catch (IOException ex) {
System.out.println("IO error Runtime. "+ex.getMessage());
}有人知道为什么它不转储/备份数据库吗?是否需要添加某些权限或其他内容(运行Windows7)。
发布于 2015-07-18 20:24:05
也许你可以看看这篇文章关于beetwen exec和命令行(http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html)的区别。
在我这边,我尝试使用以下代码来捕获mysqldump的输出--help命令,并将类StreamGoggle find放入来自javaworld的文章中:
public Main() {
try {
FileOutputStream fos = new FileOutputStream("c:/tmp/test.txt");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("mysqldump --help");
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(
proc.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(
proc.getInputStream(), "OUTPUT", fos);
errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
fos.flush();
fos.close();
} catch (Throwable t) {
t.printStackTrace();
}
}https://stackoverflow.com/questions/10732784
复制相似问题