在 Windows 上执行 Java ProcessBuilder 中的 MySQL 命令
Execute MySQL commands in Java ProcessBuilder on Windows
我正在尝试在 WINDOWS 上执行来自 Java ProcessBuilder
的一些 MySQL 命令,但我遇到了一些问题。目前,我想做的是显示数据库,但在不久的将来我将使用转储等。所以这是应该的方式。
这是我的代码:
try {
// Process process = Runtime.getRuntime().exec("cmd /c C:/\"Program files\"/MySQL/\"MySQL Server 8.0\"/bin/mysql -uroot -pMYPASS -e \"show databases;\"");
// IT DIDN'T WORK EITHER.
ProcessBuilder coolBuilder = new ProcessBuilder(new String[]{"cmd", "/c", "C://\"Program files\"/MySQL/\"MySQL Server 8.0\"/bin/mysql -u root -pMYPASS -e \"show databases;\""});
Process process = coolBuilder.start();
int errCode = process.waitFor();
if (errCode != 0) {
System.out.println("errCode = " + errCode);
errorList.add(ErrorEnum.ERROR_OCCURED_WHILE_INSIDE_SHELL.toString());
}
for (String s : output(process.getErrorStream())) {
if (!s.contains("Warning")) {
errorList.add(s);
return errorList;
}
}
return output(process.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
这是我得到的:
C://Program files/MySQL/MySQL Server 8.0/bin/mysql Ver 8.0.20 for Win64 on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: C://Program files/MySQL/MySQL Server 8.0/bin/mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
...
errCode = 1
对我做错的地方有什么建议吗?
我无法重现您的问题。我怀疑您的密码中可能有一个奇怪的字符,Windows 命令提示符不喜欢。
您是否尝试过直接执行 mysql
可执行文件而不是通过 cmd.exe
,即使用类似下面的方法?
ProcessBuilder coolBuilder = new ProcessBuilder(new String[]{"C://Program files/MySQL/MySQL Server 8.0/bin/mysql", "-u", "root", "-pMYPASS", "-e", "show databases;"});
Process process = coolBuilder.start();
这样您就不需要引用所有参数。
我正在尝试在 WINDOWS 上执行来自 Java ProcessBuilder
的一些 MySQL 命令,但我遇到了一些问题。目前,我想做的是显示数据库,但在不久的将来我将使用转储等。所以这是应该的方式。
这是我的代码:
try {
// Process process = Runtime.getRuntime().exec("cmd /c C:/\"Program files\"/MySQL/\"MySQL Server 8.0\"/bin/mysql -uroot -pMYPASS -e \"show databases;\"");
// IT DIDN'T WORK EITHER.
ProcessBuilder coolBuilder = new ProcessBuilder(new String[]{"cmd", "/c", "C://\"Program files\"/MySQL/\"MySQL Server 8.0\"/bin/mysql -u root -pMYPASS -e \"show databases;\""});
Process process = coolBuilder.start();
int errCode = process.waitFor();
if (errCode != 0) {
System.out.println("errCode = " + errCode);
errorList.add(ErrorEnum.ERROR_OCCURED_WHILE_INSIDE_SHELL.toString());
}
for (String s : output(process.getErrorStream())) {
if (!s.contains("Warning")) {
errorList.add(s);
return errorList;
}
}
return output(process.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
这是我得到的:
C://Program files/MySQL/MySQL Server 8.0/bin/mysql Ver 8.0.20 for Win64 on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Usage: C://Program files/MySQL/MySQL Server 8.0/bin/mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
...
errCode = 1
对我做错的地方有什么建议吗?
我无法重现您的问题。我怀疑您的密码中可能有一个奇怪的字符,Windows 命令提示符不喜欢。
您是否尝试过直接执行 mysql
可执行文件而不是通过 cmd.exe
,即使用类似下面的方法?
ProcessBuilder coolBuilder = new ProcessBuilder(new String[]{"C://Program files/MySQL/MySQL Server 8.0/bin/mysql", "-u", "root", "-pMYPASS", "-e", "show databases;"});
Process process = coolBuilder.start();
这样您就不需要引用所有参数。