Java导入mysql数据库/bin/bash错误
我想使用 .sql 文件和 java 创建数据库导入,然后我发现了类似这样的代码
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[]{"/bin/bash","-c","mysql -p -h localhost test < "+fileName.toString()});
,我正在使用 netbean 来运行我的桌面应用程序。然后我收到此错误消息
java.io.IOException: Cannot run program "/bin/bash": CreateProcess error=2, The system cannot find the file specified
我的问题是我在哪里可以找到 /bin/bash 路径?或者我应该做什么... 我应该配置诸如 env 变量路径之类的东西吗?
我在 Windows 上运行这个
解决方案是将 /bin/bash 替换为 cmd.exe 并将 -c 替换为 /c,但是当我执行该程序时,我的控制台上出现此消息
'mysql' 未被识别为内部或外部命令, 可运行的程序或批处理文件。
虽然我已经在Windows的PATH环境变量中设置了mysql目录
I want to create database import using .sql file with java then I found a code something like this
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[]{"/bin/bash","-c","mysql -p -h localhost test < "+fileName.toString()});
I'm using netbean to run my desktop app. then I got this error message
java.io.IOException: Cannot run program "/bin/bash": CreateProcess error=2, The system cannot find the file specified
my question is where can i find /bin/bash path? or what should i do...
should I configure something like env variable path?
I'm running this on windows
solution is replacing /bin/bash with cmd.exe and -c to /c, but when I execute the program I got this message appear on my console
'mysql' is not recognized as an internal or external command,
operable program or batch file.
though I already setup mysql directory in PATH environment variable of Windows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
/bin/bash
在 Windows 上不存在。尝试将/bin/bash
替换为cmd.exe
,并将开关-c
替换为/c
。编辑:如果您的 Java 程序似乎成功完成,但没有写入任何数据,则很可能您的 Java 程序没有等待
mysql
进程完成。尝试添加 pr.waitFor();。或者,
mysql
可能会报告错误消息或将某些内容写入其标准输出或标准错误流。如果是这种情况,您需要:NUL
。您可以通过在命令行中添加
>NUL
将标准输出重定向到NUL
,并通过添加2> 将标准错误重定向到
NUL
;NUL。我不建议丢弃输出/错误消息。如果出现错误,您如何知道?但是,很难正确处理使用 Runtime.getRuntime().exec(...) 生成的进程的标准输出和标准错误流。相反,我会使用
ProcessBuilder
。 ProcessBuilder 允许您将 mysql 进程的标准错误重定向到标准输出,这使得从两个流读取输出变得更容易(您不需要两个单独的线程)。/bin/bash
doesn't exist on Windows. Try replacing/bin/bash
withcmd.exe
, and replacing the switch-c
with/c
.EDIT: if your Java program appears to complete successfully but no data has been written, it is quite possible that your Java program didn't wait for the
mysql
process to complete. Try addingpr.waitFor();
.Alternatively,
mysql
could be reporting an error message or writing something to its standard output or standard error streams. If this is the case, you'll need to either:NUL
.You can redirect standard output to
NUL
by adding>NUL
to the command line, and redirect standard error toNUL
by adding2>NUL
.I woudn't recommend discarding the output/error messages. If there's an error, how will you know about it? However, it's difficult to properly handle the standard output and standard error streams of processes generated using
Runtime.getRuntime().exec(...)
. Instead, I would use aProcessBuilder
. A ProcessBuilder allows you to redirect themysql
process's standard error into the standard output, which makes reading the output from both streams a bit easier (you don't need two separate threads).