Search code examples
javajarruntime.execinter-process-communicat

Triggering method from a started jar File which was loaded via Runtime...exec("..") in a java file


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?


Solution

  • 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);
    
        }