0

this is what I want to do:

I need to start two jar Files from out of a java file and i want to call a method from the firstly started jar file, when i read a specific status from the second jar file. I figured out how to read the outsputstream from that jar files. (I also know, that its not the jar file who's printing out, but the classes inside the jar file. I just fomulated the question in this way to clearly explain that I use a java file in which I start two jar files)

long l = System.currentTimeMillis();
Process theProcess1 = Runtime.getRuntime().exec("java -jar \"C:/test.jar\"");

inStream = new BufferedReader(new InputStreamReader( theProcess1.getInputStream() ));  
...

I can now read the jar file's output.

On a special keyword I want the firstly started jar to run a certain method (non static).

e.g.:

if(theProcess2 output a certain statuscode)
{
   start a certain Method from executed jar file "in" theProcess1

}

I think it could be possible by using the theProcess1 output, but I don't know how to read this stream in the jar File. (The jar file doesn't know that it was started via the java file.

Any Ideas?

Kai
  • 38,985
  • 14
  • 88
  • 103
gemorra
  • 142
  • 13

1 Answers1

1

You can't access another java process classloader class definitions. See this question for how to load a jar properly : How to load a jar file at runtime

Once your jar is loaded, you can use Class.forName to access the second jar desired class

EDIT : Here is a little snippet to help you read process standard output.

//open a buffered reader on process std output
    InputStreamReader ir = new InputStreamReader(theProcess1.getInputStream());
    BufferedReader in = new BufferedReader(ir);

   //read it line per line
    String line;
    while ((line = in.readLine()) != null) {

       System.out.println(line);

    }
Community
  • 1
  • 1
user18428
  • 1,216
  • 11
  • 17
  • Hey, thanks. I need to run both jars in separate vm's. I think it must be possible to use the output stream of the Process class? But how can i use this as an input stream in my jar file? – gemorra Apr 03 '13 at 18:20
  • That's a hard constrain but you can try the method of the answer given here http://stackoverflow.com/questions/1781091/java-how-to-load-class-stored-as-byte-into-the-jvm to load a class from bytes. I think you should rethink your design by using RMI for example (or any other remote method calling strategy). – user18428 Apr 03 '13 at 19:12
  • Sorry I just realized that I may have been confused by your question formulation.Do you want a java program reacting to console output of another java program? – user18428 Apr 03 '13 at 19:17
  • I implemented a java program which is reacting to outputs from a given started jar file (via Runtime...exec("..")) and the process inputstream. What I want is to write something from the java class to the started jar file and I want that jar file to react to that. So I think it's kind of process communication what I need. I just thought that when the Process class has an input stream from which I can read the system.out.println() from the started jar, that the output stream from the Process class is for the other way around (sending sth to the jar file)... – gemorra Apr 04 '13 at 13:03
  • Or simply: By the following code: Process theProcess1 = Runtime.getRuntime().exec("java -jar \"C:/test.jar\""); theProcess1.getOutputStream(); <=== For what can I use the OutputStream and how? – gemorra Apr 04 '13 at 13:07
  • Ok i get it:) I edited my answer with a bit of code to help you read process 1 output. – user18428 Apr 04 '13 at 14:35
  • thanks.but thats not my question. ;) Again: I (and also you) used the inputStream oft the Process (getInputStream() ). I also tried it and I figured out to print out what the other process printed out via this Stream. What can I do with the OutputStream? What is : theProcess1.getOutputStream() ? – gemorra Apr 04 '13 at 14:51
  • It's the process std input.Writing to this stream is the same as inputing data in the program console – user18428 Apr 04 '13 at 16:32