I installed mysql in a docker container, and tried to run this Java code to get some variable value:
String[] cmd = new String[] {"docker", "exec", "my", "mysql", "-p123456", "-e", "select @@report_host\\G"};
Pair<Integer, String> result = this.execForExitCode(cmd);
String[] lines = result.getRight().split("\n");
log.info("getDbReportHost:");
for (String line : lines) {
log.info(line);
}
private Pair<Integer, String> execForExitCode(String[] command) {
try {
Process process = new ProcessBuilder(command).inheritIO().start();
String rst = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
return Pair.of(process.waitFor(), rst);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
But got nothing.
Instead, if I run the command directly on the console:
docker exec -it my mysql -uroot -p123456 -e "select @@report_host\G"
I get this output:
*************************** 1. row ***************************
@@report_host: 10.15.100.95
And this is what I want.
So what's wrong with my Java code?
I installed mysql in a docker container, and tried to run this Java code to get some variable value:
String[] cmd = new String[] {"docker", "exec", "my", "mysql", "-p123456", "-e", "select @@report_host\\G"};
Pair<Integer, String> result = this.execForExitCode(cmd);
String[] lines = result.getRight().split("\n");
log.info("getDbReportHost:");
for (String line : lines) {
log.info(line);
}
private Pair<Integer, String> execForExitCode(String[] command) {
try {
Process process = new ProcessBuilder(command).inheritIO().start();
String rst = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
return Pair.of(process.waitFor(), rst);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
But got nothing.
Instead, if I run the command directly on the console:
docker exec -it my mysql -uroot -p123456 -e "select @@report_host\G"
I get this output:
*************************** 1. row ***************************
@@report_host: 10.15.100.95
And this is what I want.
So what's wrong with my Java code?
Share Improve this question asked Apr 2 at 7:26 Calvin_ZCalvin_Z 1731 silver badge11 bronze badges 4 |1 Answer
Reset to default 0maybe missing -uroot
and \G
is used interactive mode, so try this
String[] cmd = new String[] {
"docker", "exec", "my", "mysql", "-uroot", "-p123456", "-e", "SELECT @@report_host;"
};
Pair<Integer, String> result = this.execForExitCode(cmd);
log.info("getDbReportHost: {}", result.getRight());
private Pair<Integer, String> execForExitCode(String[] command) {
try {
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true); // adovid thread blocked
Process process = pb.start();
// read line by line advoid , thread blocked
StringBuilder result = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
result.append(line).append("\n");
}
}
int exitCode = process.waitFor();
return Pair.of(exitCode, result.toString().trim());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
getErrorStream()
instead ofgetInputStream()
to see if this is the case. – Erich Kitzmueller Commented Apr 2 at 7:56inheritIO()
if you want to capture the command output. Notice that the answer does not use that. – Abra Commented Apr 2 at 8:02inheritIO()
redirects the output to my Java console. – Calvin_Z Commented Apr 2 at 8:14