java Runtime.exec to run interactive shell hangs - java

I try to execute windows command cmd in java, feed it with commands and print the output or error on console. However, my attempt hangs after the banner message is printed. Here is the code.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Application {
public static void main(String[] args) throws IOException, InterruptedException {
Process exec = Runtime.getRuntime().exec("cmd");
InputStream procOut = exec.getInputStream();
InputStream procErrOut = exec.getErrorStream();
OutputStream procIn = exec.getOutputStream();
new StreamConsumer(procOut).run();
new StreamConsumer(procErrOut).run();
ByteArrayOutputStream byos = new ByteArrayOutputStream();
byos.write("ping 1.1.1.1".getBytes());
byos.writeTo(procIn);
byos.flush();
procIn.flush();
int ret = exec.waitFor();
System.out.printf("Process exited with value %d", ret);
}
public static class StreamConsumer implements Runnable {
private InputStream input;
public StreamConsumer(InputStream input) {
this.input = input;
}
#Override
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
And here is the output
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
** program hangs **
Why does the program hang and no ping is executed (or printed)? I understand the streams must be consumed to avoid hanging (which I do in separate thread), but it still hangs. Has I misunderstood how the output stream is piped to interactive shell or what is the problem?

You must start threads to consume the outputs:
new Thread(new StreamConsumer(procOut)).start();
new Thread(new StreamConsumer(procErrOut)).start();

Related

ModuleNotFindError No module named 'sumolib' when I try to excecute python3 in java

When i execute the following command on the terminal
python3 main.py "J1" "Red2Way.net.xml" "i"
I receive the response that i was waiting for without problems. But when i execute the same command from java using the following code
ProcToString pts=new ProcToString("python3","main.py","J1","Red2Way.net.xml","i");
try {
System.out.println(pts.runProcess());
System.out.println(pts.hasResult());
if (pts.hasResult()){
String resultado = pts.getResult();
System.out.println(resultado);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
I get the following error
Traceback (most recent call last): File "main.py", line 2, in <module> import sumolibModuleNotFoundError: No module named 'sumolib'
I have installed sumolib in my pc for that in terminal it works perfect, i dont know why in java not.
I am using python3 and executing from NetBeans, and my os is MacOS Big Sur.
The class ProcToString have the following code
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProcToString {
private ProcessBuilder pb;
private String out="";
private Process proc=null;
public ProcToString(String... cmd){
pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
}
public int runProcess() throws IOException, InterruptedException{
proc = pb.start();
new Stream().start();
return proc.waitFor();
}
public boolean hasResult(){
return out!="";
}
public String getResult(){
return out;
}
final class Stream extends Thread{
#Override
public void run() {
StringBuilder sb=new StringBuilder();
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
try {
while((line = br.readLine())!=null){
out+=line;
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
out=sb.toString();
}
}
}
}
I try to find python and add it to the execution but i do not know where i can put the path

Setting a hotkey in a Java program that exits, without a GUI

I'm writing a program that will run continuously and I was wondering if there was a Java equivalent to the Autoit SetHotKey(Key, Action()). I saw an answer on here that related to a GUI interface, but my program does not have a GUI. I just want the program to exit whenever I press a certain key, preferably ESC.
I'd have the program running in an infinite loop using the awt.Robot's keyevents, I'd like to be able to quit the program by pressing a certain key.
There are no core Java solutions since Java was built to be as operating system agnostic as possible, and to achieve your goal, you need a program that can integrate closer to the OS. The main solutions that I know of are to integrate your program to the OS via JNA, JNI, or (my favorite), AutoIt. Of done something similar by simply having my Java program and AutoIt communicate through standard IO and sockets.
A simple example:
Java program, TrialAutoIt3a.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class TrialAutoIt3a {
// ***** of course your path to program will be different
private static final String AUTOIT_PATH = "C:/Users/Pete/Documents/Programming/AutoIt/Experiment/";
private static final String AUTOIT_EXEC = "TestWithJava.exe";
protected static final CharSequence EXIT = "exit";
private static Process proc = null;
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
System.out.println("Type \"exit\" to exit program");
try {
proc = rt.exec(AUTOIT_PATH + AUTOIT_EXEC);
} catch (IOException e1) {
e1.printStackTrace();
System.exit(-1);
}
InputStream iStream = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(iStream);
final BufferedReader bufReader = new BufferedReader(isr);
OutputStream oStream = proc.getOutputStream();
final PrintWriter pw = new PrintWriter(oStream, true);
Runnable bufReaderRunnable = new Runnable() {
public void run() {
String output;
try {
while ((output = bufReader.readLine()) != null) {
System.out.println(output);
if (output.toLowerCase().contains(EXIT)) {
proc.destroy();
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufReader != null) {
try {
bufReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
new Thread(bufReaderRunnable).start();
Runnable myRun = new Runnable() {
public void run() {
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine()) {
String line = scan.nextLine();
pw.println(line);
}
scan.close();
}
};
new Thread(myRun).start();
}
}
AutoIt program, TestWithJava.au3:
Local $line = ""
While (True)
$line = $line & ConsoleRead()
If StringInStr($line, #CR) Or StringInStr($line, #LF) Then
ConsoleWrite($line & "to java" & #CRLF)
$line = ""
EndIf
Sleep(25)
WEnd
The AutoIt program will be compiled to an exe file prior to running this program

Java Applet to ping an IP address

I made this code and it gives the following 3 errors! I need help to get away with these errors that are there in the attached image. The 3 errors that are arising aren't going away since i don't have much info about how to include system commands.
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class EventListeners extends Applet
implements ActionListener{
public void init(){
Button b = new Button("Ping");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e){
runSystemCommand(String command)
{try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}}
catch (Exception e) {
e.printStackTrace();
}}
public static void main(String[] args) {
String ip = "google.com";
runSystemCommand("ping " + ip);
}
}
![Errors][1]
You seem to be trying to write a function within a method. This is illegal in Java
Place you runSystemCommand method out side the actionPerformed method
public void actionPerformed(ActionEvent e) {
// Call runSystemCommand(...);
}
public void runSystemCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Some caveats...
Applets run in a tight security sandbox. It will NOT allow you to run system commands. Even if it, you could possible be running on a Linux or Mac box instead of Windows.
If you want to start with GUI program, start with something like JFrame, much easier to work with
I would also suggest getting your hands on a suitable IDE

Running grep From Java Program

I've spent the past 3 days without much luck on google on how to run a grep process from within Java.
I have the following code to run a grep process, however, I am only getting the first line of the response.
package com.example.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try {
Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/").start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("Exit Code: " + process.exitValue());
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am only getting the following response:
Binary file /home/user/dev/java/Parser/parser/bin/com/example/parser/Main.class matches
Exit Code: 0
When I should be getting the following response:
Binary file /home/user/dev/java/Parser/parser/com/example/parser/Main.class matches
/home/user/dev/java/Parser/parser/src/com/example/parser/Main.java:10: public static void main(String[] args) {
/home/user/dev/java/Parser/parser/src/com/example/parser/Main.java:12: Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
Exit Code: 0
I am wondering why I only get output for the first finding? Is grep forking several processes to run the search and I'm only getting a handle on the first one?
I have also tried running the process from a Thread:
package com.example.parser;
public class Main {
public static void main(String[] args) {
try {
Analyzer analyzer = new Analyzer();
analyzer.start();
analyzer.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.example.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Analyzer extends Thread {
public Analyzer() {
}
#Override
public void run() {
try {
Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("Exit Code: " + process.exitValue());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
As well as the following:
package com.example.parser;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/Parser/parser").start();
process.waitFor();
Analyzer analyzer_is = new Analyzer(process.getInputStream());
Analyzer analyzer_es = new Analyzer(process.getErrorStream());
analyzer_is.start();
analyzer_es.start();
analyzer_is.join();
analyzer_es.join();
System.out.println("Exit Code: " + process.exitValue());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.example.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Analyzer extends Thread {
InputStream is = null;
public Analyzer(InputStream is) {
this.is = is;
}
#Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
As suggested by the following article: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html
I was able to solve the issue by launching a shell with the -c flag. The following code does what I had originally intended:
package com.example.parser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
try {
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("grep -rni --include \"*.java\" \"public static void main(\" /home/user/dev/java/Parser/parser");
Process process = new ProcessBuilder(commands).start();
Analyzer analyzer_is = new Analyzer(process.getInputStream());
Analyzer analyzer_es = new Analyzer(process.getErrorStream());
analyzer_is.start();
analyzer_es.start();
process.waitFor();
analyzer_is.join();
analyzer_es.join();
System.out.println("Exit Code: " + process.exitValue());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.example.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Analyzer extends Thread {
InputStream is = null;
public Analyzer(InputStream is) {
this.is = is;
}
#Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is probably because you don't wait for the grep to finish.
Use the waitFor method :
Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", "/home/user/dev/java/").start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.waitFor();
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
Note that you may also read the output (mainly to get what happens) while it is being processed using
Process process = new ProcessBuilder("grep", "-rni", "\"public static void main(\"", String line;
while (true) {
line = reader.readLine(); // add IO exception catching
if (line != null) {
System.out.println(line);
} else {
Thread.sleep(DELAY); // DELAY could be 100 (ms) for example
}
}
I suppose you're sure a grep launched by the owner of the java program is more than one line long ?
The other reason could be your process is still running but your Java program just exited.
Use the process.waitFor(); and Read your input stream in a thread.
Start the process.
Lunch a thread with the process input stream as the input.
Now wait for the process to exit by using process.waitFor();
This might help!
Have a look at this project for grep in java https://code.google.com/p/grep4j

Using IO Streams in Java

I need to launch a binary file using Java and then interact with it using input and output streams. I've written a prototype to figure out how it works, but so far the only output I'm getting has been null. When run on its own however the child program produces output. What am I doing wrong?
import java.io.*;
public class Stream {
public static void main(String args[]) {
Process SaddleSumExec = null;
BufferedReader outStream = null;
BufferedReader inStream = null;
try {
SaddleSumExec = Runtime.getRuntime().exec("/home/alex/vendor/program weights.txt list.txt");
}
catch(IOException e) {
System.err.println("Error on inStream.readLine()");
e.printStackTrace();
}
try {
inStream = new BufferedReader(new InputStreamReader
(SaddleSumExec.getInputStream()));
System.out.println(inStream.readLine());
}
catch(IOException e){
System.out.println("Error.");
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Prompt {
//flag to end readers and writer
boolean processEnd = false;
public static void main(String[] args) {
new Prompt();
}
public Prompt() {
Process SaddleSumExec = null;
Input in = new Input(this);
Output out = new Output(this);
Input err = new Input(this);
//thread to read a write console
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
Thread t3 = new Thread(err);
try {
SaddleSumExec = Runtime
.getRuntime()
.exec(
"ConsoleApplication1/bin/Debug/ConsoleApplication1");
in.input = SaddleSumExec.getInputStream();
err.input = SaddleSumExec.getErrorStream();
out.out = SaddleSumExec.getOutputStream();
t2.start();
t1.start();
t3.start();
SaddleSumExec.waitFor();
processEnd = true;
} catch (IOException e) {
System.err.println("Error on inStream.readLine()");
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public boolean isProcessEnd() {
return processEnd;
}
public void setProcessEnd(boolean processEnd) {
this.processEnd = processEnd;
}
/*Readers of Inputs*/
class Input implements Runnable {
private BufferedReader inStream;
InputStream input;
Prompt parent;
public Input(Prompt prompt) {
// TODO Auto-generated constructor stub
parent = prompt;
}
public void run() {
inStream = new BufferedReader(new InputStreamReader(input));
while (!parent.isProcessEnd()) {
try {
String userInput;
while ((userInput = inStream.readLine()) != null) {
System.out.println(userInput);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*Writers of Output*/
class Output implements Runnable {
OutputStream out;
Prompt parent;
public Output(Prompt prompt) {
parent = prompt;
// TODO Auto-generated constructor stub
}
#Override
public void run() {
while (!parent.isProcessEnd()) {
try {
String CurLine = "";
InputStreamReader converter = new InputStreamReader(
System.in);
BufferedReader in = new BufferedReader(converter);
while (!(CurLine.equals("quit"))) {
CurLine = in.readLine();
if (!(CurLine.equals("quit"))) {
out.write((CurLine + "\n").getBytes());
out.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
You don't seem to be waiting for the child process to end so it is possible that the parent process ends before it gets a chance to read the output stream.
Here is an old but excellent article around Runtime.exec
http://www.javaworld.com/jw-12-2000/jw-1229-traps.html
The correct implementation is on this page
http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=4
From what I can tell - there could be two problems here :
Are you trying to obtain the access to the stream BEFORE the child program has started reading ?
Are you running the parent process with insufficient access rights?
If you read a null from readLine() it means the peer has closed the stream. There was no output.

Categories

Resources