Forum Stats

  • 3,759,873 Users
  • 2,251,609 Discussions
  • 7,870,847 Comments

Discussions

Process to process?

843798
843798 Member Posts: 24,864
edited Jun 30, 2001 5:38AM in Java Runtime Environment (JRE)
Hello. Could someone tell me how one Java process can communicate with another which is already running?

For example, let's say class A is sitting there in an endless loop, waiting for input. Let's say a cron job invokes class B and passes a string parameter to the main method. What would be the best way to pass that String over to class A, which is waiting for it? Or is there any way the cron job could just talk to the running class A directly, and pass it the parameter?

I know how to accomplish this with Sockets. Is there a faster, more reliable way?

Thanks,
-Erik

Comments

  • 800476
    800476 Member Posts: 40
    hmm.. In order to send some data to a running java process, you would have to get the "process" object of that Java program.

    Once you have the process object, you can get the outputstream of the process object and send any data over the stream.
  • 843798
    843798 Member Posts: 24,864
  • 843798
    843798 Member Posts: 24,864
    hmm.. In order to send some data to a running java
    process, you would have to get the "process" object of
    that Java program.

    Once you have the process object, you can get the
    outputstream of the process object and send any data
    over the stream.
    Yes, could you elaborate on how to do this?
    Also, CORBA and RMI are options, but would they perform better or worse than Sockets or File I/O?
  • 843798
    843798 Member Posts: 24,864
    see once you have create Process object for particular process you can play with that Object.U just go through thi scode .It is playing with two process communication..
    sudeepimport java.awt.*;
    import java.io.*;
    import java.util.*;

    /**
    * ProcessRunWindow
    */
    class ProcessRunWindow extends Frame {
    TextArea output;
    ProcessRunWindow (String cmd) {
    output = new TextArea(5,40);
    add(output);
    command(cmd);
    }
    /**
    * ProcessRunWindow find \"thread\" \jd\work\*.java

    */
    public static void main(String[] args) throws Exception {

    if (args.length < 1) {
    System.err.println("Usage: java ProcessRun cmd");
    System.exit(1);
    }
    String cmd = "";
    for (int i=0;i<args.length;i++) cmd += args[i] + " ";

    ProcessRunWindow prw = new ProcessRunWindow(cmd);
    prw.pack();
    prw.show();

    }

    void command (String cmd) {
    Process p=null;
    Runtime rt = Runtime.getRuntime();
    try {
    p = rt.exec(cmd);
    } catch (IOException ex) {
    System.err.println("Error on command: "+cmd+"; "+ex);
    return;
    }
    final DataInputStream dis = new DataInputStream(p.getInputStream());
    final DataInputStream des = new DataInputStream(p.getErrorStream());

    Thread thread = new Thread("input stream") {
    public void run() {
    try {
    String line=null;
    while ((line = dis.readLine()) != null) {
    if (output==null) System.out.println(line);
    else output.appendText(line+"\n");
    }
    }
    catch (IOException e) {
    System.err.println(e);
    }
    }
    };
    thread.start();

    Thread thread2 = new Thread("error stream") {
    public void run() {
    try {
    String line=null;
    while ((line = des.readLine()) != null) {
    if (output==null) System.out.println(line);
    else output.appendText(line+"\n");
    }
    }
    catch (IOException e) {
    System.err.println(e);
    }
    }
    };
    thread2.start();

    }

    public boolean handleEvent(Event event) {
    if (event.id == Event.WINDOW_DESTROY) {
    System.exit(0);
    }
    return super.handleEvent(event);
    }
    }

This discussion has been closed.