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();
Related
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());
}
}
I am a little confused about why I can't run the following command ls -l
If I run ls or pwd it works fine.
Am I missing something?
ProcessBuilder pb = new ProcessBuilder("ls -l");
pb.redirectErrorStream(true);
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ( (line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
One more question: How can I run multiple system commands concurrently? Using while loop or for loops will run the command one by one. Any advice?
Thanks in advance.
Change:
new ProcessBuilder("ls -l");
To:
new ProcessBuilder("ls", "-l");
String[] st = {"ls", "bin"};
ProcessBuilder pb = new ProcessBuilder(st);
pb.redirectErrorStream(true);
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ( (line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
Using while loop or for loops will run the command one by one.
Only when you are doing the whole start-then-read-stdout business for each one of them, one-by-one. The processes are indeed run in parallel, it's just the reading part that's stopping you from running them concurrently. All you need to do is breaking the start and read into two parts:
Stream.of(Arrays.asList("ls", "-l"),
Arrays.asList("python", "-h"),
Arrays.asList("df"))
.map(cmd->{
// Create a process for each command, but don't read the output
try {
return new AbstractMap.SimpleImmutableEntry<>(cmd,
new ProcessBuilder(cmd)
.redirectErrorStream(true)
.start().getInputStream());
} catch (IOException e) {
e.printStackTrace();
return null;
}
})
.filter(p->p!=null)
.parallel()
.forEach(in->{
// Read and print STDOUT for each process.
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in.getValue()))){
String line;
while ((line = reader.readLine()) != null) {
System.out.printf("%20s: %s\n", in.getKey(), line);
}
} catch (IOException e) {
e.printStackTrace();
}
});
The parallel() call is making the output really hard to read, it's there only to demonstrate that the processes are really running concurrently.
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
I'm trying to run a .bat file and get the output. I can run it but I can't get the results in Java:
String cmd = "cmd /c start C:\\workspace\\temp.bat";
Runtime r = Runtime.getRuntime();
Process pr = r.exec(cmd);
BufferedReader stdInput = new BufferedReader(
new InputStreamReader( pr.getInputStream() ));
String s ;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
The result is null. No idea why I get this. Note that I'm using Windows 7.
Using "cmd /c start [...]" to run a batch file will create a sub process instead of running your batch file directly.
Thus, you won't have access to its output.
To make it work, you should use:
String cmd = "C:\\workspace\\temp.bat";
It works under Windows XP.
You need to start a new thread that would read terminal output stream and copy it to the console, after you call process.waitFor().
Do something like:
String line;
Process p = Runtime.getRuntime().exec(...);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
Better approach will be to use the ProcessBuilder class, and try writing something like:
ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectInput();
Process process = builder.start();
while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
BufferedReader stdInput = new BufferedReader(new
InputStreamReader( pr.getErrorStream() ));
instead use
BufferedReader stdInput = new BufferedReader(new
InputStreamReader( pr.getInputStream ));
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.