我正在尝试使用java恢复mysql sql文件,但我不知道为什么它不起作用。代码如下。
/*NOTE: Getting path to the Jar file being executed*/
CodeSource codeSource = DBmanager.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
/*NOTE: Creating Database Constraints*/
String dbName = "dth";
String dbUser = "root";
String dbPass = "root";
String restorePath ="\""+ jarDir + "\\backup" + "\\" + s+"\"";
/*NOTE: Used to create a DOS command*/
String executeCmd = "";
executeCmd = "C:\\xampp\\mysql\\bin\\mysql -u" + dbUser + " -p" + dbPass + " --database " + dbName + " < " + restorePath;
System.out.println(executeCmd);
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
} else {
JOptionPane.showMessageDialog(null, "Error at restoring");
}
代码执行但java swing停留在运行时命令上。 System.out.println输出的行是这样的。
C:\ xampp \ mysql \ bin \ mysql -uroot -proot --database dth&lt; “F:\ Final Year Project \ Final \ build \ backup \ 0_Harish_2013-02-17-20-05-12.sql”
如果我在命令行中复制并粘贴它,这条线就可以正常工作。 Dunno为什么java swing接口只是陷入等待状态。 (相同的查询在cmd上花了2秒钟,在java上我等了5分钟。)
修改 我运行了streamgobbler但仍然没有任何好处,它仍然给出了Exit Value:1这显然是问题IllegalThreadStateException我该如何解决这个问题?
EDIT2:
当悬挂仍然存在时,狼吞虎咽没有帮助,继承人gobbler的输出
OUTPUT>C:\xampp\mysql\bin\mysql Ver 14.14 Distrib 5.5.27, for Win32 (x86)
OUTPUT>Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
OUTPUT>
OUTPUT>Oracle is a registered trademark of Oracle Corporation and/or its
OUTPUT>affiliates. Other names may be trademarks of their respective
OUTPUT>owners.
OUTPUT>
OUTPUT>Usage: C:\xampp\mysql\bin\mysql [OPTIONS] [database]
OUTPUT> -?, --help Display this help and exit.
OUTPUT> -I, --help Synonym for -?
OUTPUT> --auto-rehash Enable automatic rehashing. One doesn't need to use
OUTPUT> 'rehash' to get table and field completion, but startup
OUTPUT> and reconnecting may take a longer time. Disable with
OUTPUT> --disable-auto-rehash.
OUTPUT> (Defaults to on; use --skip-auto-rehash to disable.)
OUTPUT> -A, --no-auto-rehash
OUTPUT> No automatic rehashing. One has to use 'rehash' to get
OUTPUT> table and field completion. This gives a quicker start of
OUTPUT> mysql and disables rehashing on reconnect.
OUTPUT> --auto-vertical-output
OUTPUT> Automatically switch to vertical output mode if the
OUTPUT> result is wider than the terminal width.
OUTPUT> -B, --batch Don't use history file. Disable interactive behavior.
OUTPUT> (Enables --silent.)
OUTPUT> --character-sets-dir=name
OUTPUT> Directory for character set files.
OUTPUT> --column-type-info Display column type information.
OUTPUT> -c, --comments Preserve comments. Send comments to the server. The
OUTPUT> default is --skip-comments (discard comments), enable
OUTPUT>
(省略其余用法信息)
答案 0 :(得分:0)
我能够通过将String命令更改为String数组命令来解决问题。这使得Gobbler的使用变得多余。
答案 1 :(得分:-1)
这很可能是因为您没有从输出流中读取 - 它们会在缓冲区已满时阻塞。请阅读this
您可能希望使用类似Commons Exec的内容
您还遇到了使用管道(<
)的问题,这是一个bash结构,无效!
您需要在java中的文件中创建FileInputStream
,并将其设置为代码中的InputStream
。看看this示例。