Using IO Streams in Java - 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.

Related

How to get Live output from a consol Application in Java

I have an EXE, written in python and then convert it into an exe, When i run the exe direclty i can see the output in console realtime, but when i run the exe in java i cannot see the output until the exe complete the process.
I have search online but noting is working, Here is sample code.
package com.imviewer.ai;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FaceAnalysis implements AILibrary {
private static final Logger log = LoggerFactory.getLogger(FaceAnalysis.class);
private static final String KILL = "taskkill /F /IM ";
private static final String EXE_CPU = "FaceAnalysis-CPU.exe";
private static final String EXE_GPU = "FaceAnalysis-GPU.exe";
List<String> paths = new ArrayList<>();
public FaceAnalysis() {
}
#Override
public void setArguments() {
log.info("Setting arguments");
paths.add(new File(AIUtils.FACE, EXE_CPU).getAbsolutePath());
paths.add("--images=yes");
paths.add("--videos=yes");
paths.add("--csv=no");
paths.add("--folderpath=\"Face\"");
paths.add("--algo=cnn");
paths.add("--gpu=no");
paths.add("--cpus=6");
paths.add("--debug=no");
paths.add("--fpstoproces=5");
paths.add("--upsample=0");
paths.add("--onlydetection=yes");
paths.add("--savefullimages=no");
paths.add("--savefaceimage=no");
paths.add("--enablebox=no");
paths.add("--maxarea=640000");
paths.add("--imageextensions=jpg,png,jpeg,tiff,raw,bmp");
paths.add("--videoextensions=mp4,avi,flv,mpeg,mpg,wmv");
paths.add("--age=1");
paths.add("--gender=1");
paths.add("--expression=1");
}
#Override
public void run() {
}
#Override
public void runEXE() {
log.info("Running Face Exe");
ProcessBuilder builder = new ProcessBuilder(paths);
builder.directory(new File(AIUtils.FACE));
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectErrorStream(true);
Process proc;
try {
proc = builder.start();
proc.getOutputStream().close();
// Any error message?
Thread errorGobbler
= new Thread(new StreamGobbler(proc.getErrorStream(), System.err));
// Any output?
Thread outputGobbler
= new Thread(new StreamGobbler(proc.getInputStream(), System.out));
errorGobbler.start();
outputGobbler.start();
// Any error?
int exitVal = proc.waitFor();
errorGobbler.join(); // Handle condition where the
outputGobbler.join(); // process ends before the threads finish
log.info("Exit Value: "+ exitVal);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("End Running Face EXE");
}
#Override
public boolean checkAlreadyRunning() {
// TODO Auto-generated method stub
return false;
}
#Override
public void close() {
// TODO Auto-generated method stub
}
class StreamGobbler implements Runnable {
private final InputStream is;
private final PrintStream os;
StreamGobbler(InputStream is, PrintStream os) {
this.is = is;
this.os = os;
}
public void run() {
try {
int c;
while ((c = is.read()) != -1)
os.print((char) c);
} catch (IOException x) {
// Handle error
}
}
}
public static void main(String[] args) {
FaceAnalysis face = new FaceAnalysis();
face.setArguments();
face.runEXE();
}
}
Try the approach like this:
public static void main(String[] args) throws IOException {
Process p = new ProcessBuilder().command("ping", "webelement.click").start();
InputStream i = p.getInputStream();
Scanner scanner = new Scanner(i);
while (scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}
}

Undefined behavior when working with the input stream

Why is this code contains undefined behavior (the program is completed in half the time). How do I make a clean shutdown of the reader when I need to complete the program?
public class Main implements Runnable {
private static BufferedReader reader;
public static void main(String[] args) {
reader = new BufferedReader(new InputStreamReader(System.in));
new Thread(new Main()).start();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
//System.in.close(); // <-- undefined behavior
reader.close(); // <-- undefined behavior
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
try {
reader.readLine();
} catch (IOException ignore) {
}
}
}

How to Stop Producer Thread and Consumer Thread in case FileNotException occurs

I am reading a csv file from a location.
Could you please tell me how can I stop the Producer Thread and Consumer Thread incase file is not found in this case ?
Below is my program which creates two threads, Producer and Consumer Threads to read the data from the file
package com.util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import au.com.bytecode.opencsv.CSVReader;
public class TestProgram {
public static void main(String args[]) {
final String startToken = ",Nifty 50 Gainers";
final String endToken = "50 Losers";
final PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReaderTmp = null;
try {
pipedReaderTmp = new PipedReader(pipedWriter);
} catch (IOException e) {
}
final PipedReader pipedReader = pipedReaderTmp;
// Consumer
new Thread(new Runnable() {
#Override
public void run() {
try {
CSVReader csvReader = new CSVReader(pipedReader);
while (true) {
String[] line = csvReader.readNext(); // blocks until the next line is available
if (line == null)
break; // end of stream has been reached
if (line != null && line.length > 3) {
String indices_name = line[1];
if (indices_name != null) {
System.out.println(indices_name);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
// Producer
new Thread(new Runnable() {
#Override
public void run() {
try {
BufferedReader br = new BufferedReader(new FileReader(
"C:\\Users\\ravikiranv\\Downloads\\MA050116.csv"));
String line = null;
while ((line = br.readLine()) != null) {
if (startToken.equals(line))
break;
}
while ((line = br.readLine()) != null) {
if (line.contains((endToken))) {
break;
} else {
pipedWriter.write(line + '\n');
}
}
pipedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
See the Javadoc for PipedReader.read().
Throws:
IOException - if the pipe is broken, unconnected, closed, or an I/O error occurs.
So just close it and the other end wwil get an IOException.
Note: The PipedReader has some issues - you may find it safer/better to use something like a `BlockingQueue.
You can and should instantiate and start the Threads only if you know the file exists. This keeps you safe from the concerns to stop threads from within.
File file = new File("C:\\Users\\ravikiranv\\Downloads\\MA050116.csv");
if (file.exists()) {
new Thread(new Runnable() { ... }).start();
...
}
If you don't want that for a reason, you could keep a reference to the consumer thread and stop it from within the producer. For an example how to do that, see this answer

Program in java that reads data from a text file

date time kg
12/10/2013 00.00.01 1
13/11/2013 00.00.05 2
17/12/2013 00.00.90 5
21/12/2013 00.00.23 6
27/12/2013 00.00.43 9
I have these data in an txt file. I would like to make o program in java that would read these data. I ' ve written the code above but I have mistakes. Could someone help me? The data have space between each other.
import java.io*;
public class ReadTextfile{
public static void main (String[] args) {
File file = new File ("test.txt");
StringBuilder line = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader (new FileReader(file));
String text = null;
while ((text = reader.readLine()) !=null) {
line.append(text)
.append(System.getProperty ("line.separator"));
}
}
catch (IOException e) {
e.printStackTrace();
catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
if (reader !=null){
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(line.toString());
}
}
boy you are only having some syntax problem
1 : replace
import java.io* with import java.io.*
2 : take care of your catch body being started and closed properly
try
{
// your code
}
catch(Exception e)
{
}
here is the working code , compare your program
import java.io.*;
public class ReadTextfile{
public static void main (String[] args)
{
File file = new File ("C:/Users/hussain.a/Desktop/test.txt");
StringBuilder line = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader (new FileReader(file));
String text = null;
while ((text = reader.readLine()) !=null) {
line.append(text)
.append(System.getProperty ("line.separator"));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
if (reader !=null){
reader.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
System.out.println(line.toString());
}
}
catch (FileNotFoundException e)
This is unreachable code, since above it you caught IOException.
Note that:
public class FileNotFoundException extends IOException
Your code won't compile. Remove this catch (You didn't even close it..)
Another thing, if this is not a type, you should replace java.io* with import java.io.*.
I would take the following approach:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadTextFile
{
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("test.txt");
Scanner scanner = new Scanner(file);
List<Result> results = new ArrayList<Result>();
while(scanner.hasNextLine())
{
String currentLine = scanner.nextLine();
String [] resultArray = currentLine.split(" ");
results.add(new Result(resultArray[0], resultArray[1], resultArray[2]));
}
scanner.close();
}
private static class Result
{
private String date;
private String time;
private String kg;
public Result(String date, String time, String kg)
{
super();
this.date = date;
this.time = time;
this.kg = kg;
}
public String getDate()
{
return date;
}
public String getTime()
{
return time;
}
public String getKg()
{
return kg;
}
}
}
Now you can pull out any information that you want to from the list of results that you have.
So if you wanted to print everything, you could do the following:
for(Result singleResult : results)
{
System.out.println(singleResult.getDate() + " " + singleResult.getTime() + " " + singleResult.getKg());
}
You basically can do whatever you want to with the data. This approach would also allow you to transform the data into different types before you even create the Result object.

Problem getting output and passing input to a executing process running under java

I am trying to call a simple program test.exe which is as simple as-
int main()
{
int a;
cout<<"Welcome\n";
while(cin>>a&&a!=0)
cout<<"you entered "<<a<<endl;
}
I want to run it from a java program as a process, and send+recieve i/o from it. I am using the process with 2 threads as follows-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Processproblem {
public static void main(String[] args)throws IOException, InterruptedException {
final Process process;
try {
process = Runtime.getRuntime().exec("test.exe");
} catch (IOException e1) {
e1.printStackTrace();
return;
}
new Thread(new Runnable() {
public void run() {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
while ((line = br.readLine()) != null) {
System.out.println("[OUT] " + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
try {
byte[] buffer = new byte[1024];
int reading=0;
System.out.println(reading);
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
while(reading!=-1)
{
reading= System.in.read(buffer);
for(int i = 0; i < buffer.length; i++) {
int intValue = new Byte(buffer[i]).intValue();
if (intValue == 0) {
reading = i;
break;
}
else
{
bw.append((char)intValue);
}
}
bw.newLine();
bw.flush();
}
} catch (Exception e) {
}
}
}
).start();
}
}
But they are not working as expected. When i run the program it just shows the "Welcome\n" message and then stops for input. When i give a integer and press enter in the java console it does nothing.
What am I doing wrong? They are two separate threads so why are they blocking each other? Is there any problem in my concept?
The program waits for your input. Grab the process output stream (using getOutputStream) and write to it.

Categories

Resources