-2

How can I get an input from another java program? I have one class that generates a random graph and writes it in the windows cmd console with System.out.println();. What I want is this other class to read this output and use it to do some calculations. Both classes are working fine - all I need to know is how to get this output from the first class.

EDIT: I want this http://pastebin.com/GnsUZVht to read the output that this http://pastebin.com/cgXMCbgb do to put it back in a matrix.

Nuno
  • 515
  • 1
  • 5
  • 21
  • 2
    do you want to read the output from another java program or output done by some other class in the same program? Please specify your question better. – peshkira Jun 24 '13 at 20:36
  • Please provide an example that illustrates what you want to do. You should include how you expect to run your program. – Code-Apprentice Jun 24 '13 at 20:39
  • It's pretty clear from the question that Nuno wants to write the result of one program out to the terminal, where it can be picked up by a second program. – Nathaniel Ford Jun 24 '13 at 20:42

3 Answers3

2

Take a look at the solution here.

According to that, you can use the Process Builder , which is used to create operating system processes.

The example:

ProcessBuilder   ps=new ProcessBuilder("java.exe","-version");

ps.redirectErrorStream(true);

Process pr = ps.start();  

BufferedReader in = new BufferedReader(new 
InputStreamReader(pr.getInputStream()));
String line;

while ((line = in.readLine()) != null) {
    System.out.println(line);
}
pr.waitFor();
in.close();
System.exit(0);
Community
  • 1
  • 1
aran
  • 10,978
  • 5
  • 39
  • 69
0

It would be easiest to just have both classes in the same program and then store the data that is generated from the first class in a variable and then pass those variables as arguments into the next class rather than printing and reading from console.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
0

Supposing your want do something like (e.g. in the linux bash)

java -jar one.jar | java -jar two.jar | ...

The the code in one.jar could write to System.out whereas the code in two.jar then reads from System.in.

clearwater
  • 514
  • 2
  • 7