How to execute commands inside a shell using Process object in Java - java

I executed a shell using:
Process process = Runtime.getRuntime().exec("docker exec rabbitmq bash");
But now, I want to execute another command inside the opened shell:
Writer w = new OutputStreamWriter(process.getOutputStream(), "UTF-8");
w.write("rabbitmqctl list_queues\n");
w.flush();
But I can't get the output of rabbitmqctl list_queues
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
I don't get any errors, if I use a malformed command, it will issue an error message:
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorString = error.readLine();
System.out.println("\nError : " + errorString);
int exitCode = process.waitFor();

The problem was the lack of use of -i parameter:
Process process = Runtime.getRuntime().exec("docker exec -i rabbitmq bash");
But, using only Process class I had had problems, the interpreter didn't understand the end of output and the loop didn't stop:
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
But using ProcessBuilder it worked:
#Test
void test_process_builder() throws IOException {
ProcessBuilder builder = new ProcessBuilder("docker","exec","-i","rabbitmq3","bash");
Process process = builder.start();
OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
BufferedReader error = new BufferedReader(new InputStreamReader(stderr));
writer.write("rabbitmqctl list_queues \n");
writer.flush();
writer.close();
Scanner err = new Scanner(stderr);
while (err.hasNextLine()) {
System.out.println(err.nextLine());
}
Scanner scanner = new Scanner(stdout);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}

Related

interacting with linux shell prompt in Java

Executor exec = new DefaultExecutor();
exec.setWorkingDirectory("/var/java/apache-tomcat-7.0.47/webapps/Telegram/tg")
CommandLine cl = new CommandLine("bin/telegram-cli -k tg-server.pub -W -U root");
int exitvalue = exec.execute(cl);
How can I get output of this command:
exec.execute(cl);
and run other related commands on telegram-cli command prompt e.g. contact_list, msg contact "Hello world";
It looks to me as though it would be easier just to use ProcessBuilder instead.
ProcessBuilder pb = new ProcessBuilder("bin/telegram-cli","-k","tg-server.pub","-W","-U","root");
pb.directory(new File("/var/java/apache-tomcat-7.0.47/webapps/Telegram/tg"));
Process p = pb.start();
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(p.getOutputStream()),true)) {
String line = null;
pw.println("contact_list");
while ((line = br.readLine()) != null) {
System.out.println("line = " + line);
}
}
Update
It looks like the writes with PrintWriter need to be flushed. You can do this by calling pw.flush() or adding the second argument true to the PrintWriter constructor. I don't know the commands for telegram-cli, but you need to add one that will produce an output you can use to identify when to quit. Here's an example using /bin/sh.
ProcessBuilder pb = new ProcessBuilder("/bin/sh");
Process p = pb.start();
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(p.getOutputStream()),true)) {
String line = null;
pw.println("ls");
pw.println("pwd");
pw.println("echo quit"); // this gives me output I can test for to break the loop
pw.flush();
while ((line = br.readLine()) != null && !line.contains("quit")) {
System.out.println("line = " + line);
}
}
int retcode = p.waitFor();
System.out.println("process ended with " + retcode);
Produces this output:
line = build
line = build.xml
line = dist
line = manifest.mf
line = nbproject
line = src
line = /home/shackle/NetBeansProjects/JavaApplication20
process ended with 0

capture output of a running process in Java

(I'm using Windows 7) I know how to start a process, pass some arguments and read the output of that process.
import java.io.*;
class Test {
public static void main(String args[]) throws IOException {
ProcessBuilder pb
= new ProcessBuilder("java", "ProgramFoo", "ArgBar");
Process process = pb.start();
final InputStream is = process.getInputStream();
BufferedReader reader
= new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
But what I want to do, is to read the output of a running process. Maybe by its PID or name.
Any Ideas?
Check out http://commons.apache.org/proper/commons-exec/ for better handling of processes.
Alternatively, you could get an InputStream from the process you already have and read it like this:
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
stdout = br.readLine();
while (stdout != null) {
stringBuffer.append(stdout);
stdout = br.readLine();
}

Java: Pass values to the jar using OutputStreamWriter

I'm trying to execute myfile.jar using another java program which as follows. When the myfile.jar is executed standalone in console, it will as two questions:
Start the load process (y/n)?
Start the patch process (y/n)?
I will pass y to 1st and n to 2nd question. The same thing I'm trying to do using the following java program and it successfully passes answer to the 1st question, but waiting at the 2nd question.
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "myfile.jar", "arg1");
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream in = p.getInputStream();
InputStreamReader ins = new InputStreamReader(in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream());
BufferedReader br = new BufferedReader(ins);
String line = null;
out.write("y\n");
out.write("n\n");
out.flush();
while ((line = br.readLine()) != null) {
System.out.println(line);
}
out.close();
p.waitFor();
p.destroy();
Note: The myfile.jar uses Scanner to take the input from the user.
I guess I found the solution to my question. For future reference, the following is the final program that I've used to solve my problem:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "myfile.jar", "arg1");
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream in = p.getInputStream();
InputStreamReader ins = new InputStreamReader(in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream());
BufferedReader br = new BufferedReader(ins);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
if (line.contains("Start the load process (y/n)?")) {
out.write("y");
out.newLine();
out.flush();
}
if (line.contains("Start the patch process (y/n)?")) {
out.write("n");
out.newLine();
out.flush();
}
}
out.close();
p.waitFor();
p.destroy();

java input.readLine hangs after starting a powershell script

We are trying to call a Powershell script via Java but it hangs when we try to read the output of that script. It hangs on "input.readLine()"
Here is a code we have tried:
String command = "cmd /c powershell C:\\_checkouts\\TestPowerShell\\passwordExpirationTime.ps1";
Process p = Runtime.getRuntime().exec(command);
new InputStreamReader(p.getInputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();;
for (String line = input.readLine(); line != null; line = input.readLine()) {
stringBuilder.append(line);
}
input.close();
String msg = stringBuilder.toString();
System.out.println("msg: " + msg);
We tried looking at this solution Java program hangs when trying to invoke powershell script
but none of those suggestions worked for us.
We also tried it without cmd /c and with cmd /k. We really want this as a generic class where any script could be called. i.e. BAT, BASH, SH, Powershell, etc.
Any ideas?
Thanks to the answer below here is the code that worked:
try{
final ProcessBuilder pb = new ProcessBuilder("powershell","C:\\psFile.ps1");
pb.redirectInput(Redirect.from(new File("NUL")));
final Process p = pb.start();
final int retcode = p.waitFor();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch (Exception e){
e.printStackTrace();
}
Here's some additional information. Our powershell was returning an error because it was restricted. That is why we needed all the Redirect to NUL. Once we fixed that and it wasn't returning an error we were able to simplify our code to this:
try{
final ProcessBuilder pb;
pb = new ProcessBuilder(args);
final Process p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch (Exception e){
e.printStackTrace();
}
Use a ProcessBuilder. Any process implying a modicum of I/O should outright refuse to run via Runtime.exec(), alas, it cannot detect that. And Oracle hasn't flagged it as #Deprecated which is a pity.
Anyway:
final File logFile = new File("/path/to/logfile");
final ProcessBuilder pb = new ProcessBuilder("powershell", "/path/to/the/ps1");
pb.redirectOutput(Redirect.to(logFile));
pb.redirectErrorStream(true);
final Process p = pb.start();
final int retcode = p.waitFor();
// deal with retcode
// read result from the log file

Java run Windows command line command returning a value

I would like to run a windows command line command from java and return the result into java. Is this possible?
for example, I would like to do the following
Object returnValue = runOnCommandLine("wmic cpu get LoadPercentage");
//In this case, returnValue is the cpu load percent as a String
Edit: I was able to get this working
InputStream inputStream = new ProcessBuilder("wmic", "cpu", "get", "status").start().getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
String theString = writer.toString();
System.out.println("My string: " + theString);
Data you need is commandOutput.
String cmd = "wmic cpu get LoadPercentage";
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader stdin = new BufferedReader(
new InputStreamReader(p.getInputStream()));
StringBuilder commandOutput = new StringBuilder();
String line;
while ((line = stdin.readLine()) != null) {
commandOutput.append(line);
}
int exitValue = -1;
try {
exitValue = p.waitFor();
} catch (InterruptedException e) {
// do something here
}
You could do the following:
Process proc = Runtime.getRuntime().exec("net start");
InputStreamReader isr = new InputStreamReader(proc.getInputStream());
BufferedReader br = new BufferedReader(isr);
String temp = null;
while (( temp = br.readLine() ) != null)
System.out.println(temp);
Take a look into ProcessBuilder.
Below Java 1.5 Runtime.getRuntime().exec(...) was used.

Categories

Resources