Java导入mysql数据库/bin/bash错误

发布于 2024-12-11 06:11:44 字数 671 浏览 0 评论 0原文

我想使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-12-18 06:11:44

/bin/bash 在 Windows 上不存在。尝试将 /bin/bash 替换为 cmd.exe,并将开关 -c 替换为 /c

编辑:如果您的 Java 程序似乎成功完成,但没有写入任何数据,则很可能您的 Java 程序没有等待 mysql 进程完成。尝试添加 pr.waitFor();。

或者,mysql 可能会报告错误消息或将某些内容写入其标准输出或标准错误流。如果是这种情况,您需要:

  1. 读取有问题的流,或者
  2. 如果您确定可以忽略它,请将有问题的流重定向到 NUL

您可以通过在命令行中添加 >NUL 将标准输出重定向到 NUL,并通过添加 2> 将标准错误重定向到 NUL ;NUL。

我不建议丢弃输出/错误消息。如果出现错误,您如何知道?但是,很难正确处理使用 Runtime.getRuntime().exec(...) 生成的进程的标准输出和标准错误流。相反,我会使用 ProcessBuilder。 ProcessBuilder 允许您将 mysql 进程的标准错误重定向到标准输出,这使得从两个流读取输出变得更容易(您不需要两个单独的线程)。

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "mysql -p -h localhost test < "+fileName.toString());
builder.redirectErrorStream(true);
Process pr = builder.start();

// Get mysql process's standard output/error.
InputStream is = pr.getInputStream();

// Now read from it and write it to standard output.
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (line != null) {
    System.out.println(line);
    line = reader.readLine();
}

/bin/bash doesn't exist on Windows. Try replacing /bin/bash with cmd.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 adding pr.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:

  1. read the offending stream(s), or
  2. if you're sure you can ignore it, redirect the offending stream(s) to NUL.

You can redirect standard output to NUL by adding >NUL to the command line, and redirect standard error to NUL by adding 2>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 a ProcessBuilder. A ProcessBuilder allows you to redirect the mysql 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).

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "mysql -p -h localhost test < "+fileName.toString());
builder.redirectErrorStream(true);
Process pr = builder.start();

// Get mysql process's standard output/error.
InputStream is = pr.getInputStream();

// Now read from it and write it to standard output.
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (line != null) {
    System.out.println(line);
    line = reader.readLine();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文