getInputStream() is not returning readable information - java

My homework assignment is to call a .jar from within a java program but I can't get the input stream to return the results into something readable. Here is what I did first:
InputStream in = proc.getInputStream();
System.out.println(in);
But that didn't work, and I found some different variation:
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(input);
But that didn't work either, both scenarios it returned something like this: java.io.BufferedReader#2ce908. How can I get it to return a readable output?
EDIT: This is a java program receiving the output from another java program. The program that the user starts is called Translate.java which takes in English words and passes them as command line arguments using the Runtime.getRuntime().exec("java -jar Dictionary.jar"+) command. I was told to use the getInputStream() in Translate.java to receive the output from the Dictionary.java program.

A stream is not text; it is a thing that reads a file for you. You have to call readLine:
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = null;
while ((s = input.readLine()) != null) { // it returns null at the end of the file
System.out.println(s);
}

Related

Java Runtime.getRuntime().exec Executing only first line

I want to execute a python script from java.
The code is getting in to the python file but only executes first line of the file.
following is the code:
Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
value = in.readLine();
after the first line nothing is executed.
what is the solution?
'dir' value is getting from
final String dir = System.getProperty("user.dir");
link to python file:
https://drive.google.com/file/d/1tvkFTM_Oo5gTS7FyzeNgoeY5DLitFQjD/view?usp=sharing
The problem seems, that you are only reading the first line of your BufferedReader. So change your code as follows:
Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
it worked fine when I passed like this:
String cmd = "python2.7 "+dir+"/pyfiles/getGitFiles.py "+ownerVal+" "+repoVal+" "+folderVal+" "+branchVal+" "+Values.accessToken;
System.out.println(cmd);
Process p = Runtime.getRuntime().exec(cmd);
passing the arguments inside the 'exec' itself is causing the problem.

read all lines from string buffered reader

I have simple client/server program that sends and recieves strings from client to server and vice versa.
Some string contain newline characters "n\", eg "ERR\nASCII: OK"
my buffered reader:
BufferedReader in =
new BufferedReader(
new InputStreamReader(ConverterSocket.getInputStream()));
I am trying to display each line in the string to the user/ client.
I have tried the following for loop:
for (line = in.readLine(); line != null; line = in.readLine()){
System.out.println(line);
}
output (as expected):
ERR
ASCII: OK
but the loop doesn't end. I have also tried:
while ((line = in.readLine()) != null){
system.out.println(line)
}
which also doesn't end properly.
How can i print all lines in the string?
The readLine() method returns null at end of stream, which doesn't occur until the peer closes the connection.

How do I only send the output and not the input to InputStreamReader when executing a shell command using ProcessBuilder?

I am executing an executable with a command line argument using ProcessBuilder and I am trying to read the output using a BufferdReader. However, when I print out the input stream of the process, it seems I am first printing out the output, then the input as well.
For example, I am trying to execute "my_command -an-option /path/to/file", and when I print out the buffered reader, I am printing out the output followed by the contents of the my file at /path/to/file. I guess it makes sense that the input stream is reading in my inputp and the output,
public static void d(String file) throws Exception {
ProcessBuilder builder = new ProcessBuilder("my_command", "-an-option", file);
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.waitFor();
String s = null;
while ((s = in.readLine()) != null) System.out.println(s);
in.close();
}
public static void main(String[] args) {
d("/path/to/file");
}
Does anyone know how to make it only print out the output? I want to save the output to a string or something and parse it, etc.

Looping over InputStream truncating data

So this is a very simple problem with a simple solution that I'm just not seeing:
I'm trying to get a list of data through an InputStream, looping until I reach the end of the stream. On each iteration, I print the next line of text being passed through the InputStream. I have it working but for one small problem: I'm truncating the first character of each line.
Here's the code:
while (dataInputStream.read() >= 0) {
System.out.printf("%s\n", dataInputReader.readLine());
}
And the output:
classpath
project
est.txt
Now, I know what's going on here: the read() call in my while loop is taking the first char on each line, so when the line gets passed into the loop, that char is missing. The problem is, I can't figure out how to set up a loop to prevent that.
I think I just need a new set of eyes on this.
readLine for DataInputStream is deprecated. You may try wrapping it with a BufferedReader:
try
{
String line;
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( dataInputStream ) );
while( (line = bufferedReader.readLine()) != null )
{
System.out.printf("%s\n", line);
}
}
catch( IOException e )
{
System.err.println( "Error: " + e );
}
Also, I`m not sure, that it is a good idea to use available() due to this specification:
* <p>Note that this method provides such a weak guarantee that it is not very useful in
* practice.
Use one BufferedReader and InputStreamReader, here is one example:
InputStream in=...;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while (br.ready()) {
String line = br.readLine();
}
dataInputStream.read() reads the first character of the InputStream, the same as dataInputReader.readLine() reads the complete next line. Every read character or line is then gone. you can use the dataInputStream.available() to check if the InputStream has data available.
That should print the correct output:
while (dataInputStream.available()) {
System.out.printf("%s", dataInputReader.read());
}
String line;
while ((line = dataInputReader.readLine()) != null) {
System.out.println(line);
}

Writing the Output of an Operating System Command to the Console

I want to execute an operating system command in Java, and then print out it's returned value. Like this:
This is what I am trying...
String location_of_my_exe_and_some_parameters = "c:\\blabla.exe /hello -hi";
Runtime.getRuntime().exec(location_of_my_exe_and_some_parameters);
I tried putting a System.out.print() on the beginning of my Runtime... line, but it failed. Because, apparently, getRuntime() returns a Runtime object.
Now, the problem is, when I execute the "blabla.exe /hello -hi" command in command line, I got a result like: "You executed some command, hurray!". But, in Java, I got nothing.
I tried putting the return value into a Runtime object, to an Object object. However, they both failed. How can I accomplish this?
Problem Solved - this is my solution
Process process = new ProcessBuilder(location, args).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);
}
Notice that Runtime.exec(...) returns a Process object. You can use this object to capture its input stream and retrieve whatever it prints to the standard output:
Process p = Runtime.getRuntime().exec(location_of_my_exe_and_some_parameters);
InputStream is = p.getInputStream();
// read process output from is
You can capture the output of a command using this:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
log.info(line);
}
//This will wait for the return code of the process
int exitVal = pr.waitFor();
UseProcessBuilder instead of Runtime.
Like:
Process process = new ProcessBuilder("c:\\blabla.exe","param1","param2").start();
Answer:
Process process = new ProcessBuilder("c:\\blabla.exe","/hello","-hi").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));

Categories