Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass a string to another program 1

Status
Not open for further replies.

Outy

Programmer
Feb 7, 2001
16
BE
Hello,

I've asked this before but still having problems...

What I want to do is call another javaprogram and pass a string to the other program.
So when I'm having a java-application running for example Sender.class, next I want to call another program named Reciever.class. Then a String("this is a test") from the "Senderprogram" has to be given to the "Recieverprogram".
Is this possible? Can u give me an example?


Help will be appreciated!


Thx
Outliner s-)
 
Hi Outliner,
Here we go...

This is the sender class that calls another java program (implements a simple frame):

import java.io.*;
public class Sender extends Thread
{
public static void main(String[] argv)
{
new Sender();
}

public Sender()
{
String[] args = {"java", "Receiver", "Parameter From Sender"};

try
{

Process p = Runtime.getRuntime().exec(args);
_br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String reply = _br.readLine();
System.out.println(reply);
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
}
private BufferedReader _br;
}

- Getting Process object is not necessary, I used it only to get System.out streams from called program.
------------------------------------

And here is Receiver class:

import java.awt.*;
public class Receiver extends Frame
{
public static void main(String[] argv)
{
if(argv.length > 0)
{
System.out.println("Received parameter: " +argv[0] + ". Opening frame...");
new Receiver(argv[0]);
}
else
{
System.out.println("Argument needed!!!");
}
}

public Receiver(String receivedParam)
{
super(receivedParam);

this.setSize(300,200);
this.show();
}
}

So, Sender program gives parameter and that parameter is used to set the title of the frame.
Note, that if you have a receiver program which has no GUI, the its just a background-process (dos-promt window is not shown) but you can get the Input/OutputStreams by using Process-class.

I hope this helped you!
 
Hi again,

If I have defined my program what I want to start in a String called program and my "message to be sent" in a String called sendstring.
Is it still possible to send the string "sendstring" to the program Print(javaprogram)?
Here is the situation:

public class Sender extends Thread
{
public static void main(String[] argv)
{
new Sender();
}

public Sender()
{
String program = "Print";
String sendstring = "this is a test!";
try
{
Process p = Runtime.getRuntime().exec(program);
System.out.println("Testing");
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
System.out.println("failed");
}
}
}
//////////////////////
class Print
{
public static void main(String[] args)
{
System.out.println(receivedmessage);
//where recievedmessage is the string recieved from the Sender.class
}
}

Thanks

Outliner
 
Bacause that program you are starting is a java-program you have to start it using "java Print".
And because exec-method you are using is defined like:
exec(String[] cmdarray), you have to use StringArray as parameter.

So you could do it like this:

// Here is the modifications for constructor:
public Sender()
{
String[] cmdArray = new String[3];

String program = "Print";
String sendstring = "this is a test!";

cmdArray[0] = "java"; // Because program have to be started with java XXX
cmdArray[1] = program; // name of the program
cmdArray[2] = sendString; // String to be sent

try
{
Process p = Runtime.getRuntime().exec(cmdArray);
System.out.println("Testing");
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
System.out.println("failed");
}
} // <-- Sender constructor

---------------------------------

And here could be the Print-class:
class Print
{
public static void main(String[] args)
{
// Args[0] contains the parameter from got from Sender class
System.out.println(args[0]);
}
}


Ask if you have more questions about the issue ;)

-Vepo
 
Hi Vepo,

Is it possible to wait till the Print program has send a message(string) back to the Sender program and then the Sender program shows this &quot;replymessage&quot; on the screen?

Thanks Vepo

Greetz
Outliner


 
Hi again Outliner,

Yes it's possible in many ways... it depends on the requirements of your program.

Maybe the easiest (maybe not the best) way to do this is to get InputStream from the process you started (Look at the first reply).
When you do this, Printer program's OutputStreams are received by Sender.

Here is an example how you could do it (this is related to the first reply I wrote):


Process p = ..... // execute&get the process here
BufferedReader _br = new BufferedReader(new InputStreamReader(p.getInputStream())); // Get process's InputStream

try
{
String reply = new String(); // outputstream rode
boolean replyReceived = false; // is reply received
while(!replyReceived)
{
reply = _br.readLine();
// If received String that starts REPLY:
if(reply.startsWith(&quot;REPLY:&quot;))
{
System.out.println(&quot;Reply received: &quot; + reply);
replyReceived = true; // End loop
}
}

}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}

______________________________________________

Here is the modifications to Print class (Still look at the code in first reply...):

public Print(String receivedParam)
{
super(receivedParam);

this.setSize(300,200);
this.show();
System.out.println(&quot;REPLY: Thanks for your parameter&quot;);
}

________________________________

So this program acts like this:
It starts Print program that receives parameter from Sender.
After Print has received the parameter it uses System.out.println to print out the reply (Sender listens that stream with BufferedInputStream type object).

The bad thing about this implementation is that you have to use System.out to print that reply... but on the other hand when you do it like this you dont have to open any sockets for communication.

Notice that in this code, if Print-program doesn't print that reply, the sender will be in endless loop...

Hope you get it work!
 
Hi Vepo,

I'll explain the whole program I have to make
The program I have to make is a messagebroker, It has to connect to MQSeries(program from IBM that runs on &quot;networklayer&quot;, it sends and receives messages on queue's). So I recieve a message from MQS, I have to read the 4 first characters of the message(string), these characters represent a short name of the program I have to start. I take the 4 characters and look into a file containing the names of all the programs. If I find the right program, I start it and pass the rest of the string also to the program. Next I have to wait for a replymessage from the started program. Receive it and put it back on the queue.
Now what would be the best way to send and recieve the strings to and from the started messages?

By the way the privious codes work fine, thx

Greetz
Outliner
 
Hi,

Well, when I thought more about this, I found these ways to receive string from another running java-process:

* RMI - Unfortunatelly I don't know RMI ;) (But you can check it out f.ex. from If you use rmi (I'm really not sure if it suits for this..)
the Sender class should be server and Print class client...

* Using sockets, you could for example listen some port at Sender class and send a string to that port from Print class (I'd use UDP connection)

* Using the way I mentioned before (get Stream from process). That may after all be the best way... atleast the easiest.

* You could also write that reply to some file that Sender program would read. Sender prog could for example check the file's lastModified-value and if it's changed (the file has been modified -> response has been received) and then read the response from the file.


Another sollution that popped in my mind (It may not work as it should) is usage of the java's reflection methods. So you could invoke a class using class name (the parameter you receive).
It would look like something like this:
Class c = Class.forName(nameOfTheClass);
....

The problem here is that is had to be casted to the right class.
(e.g. Class c = Class.forName(&quot;java.lang.String&quot;);
(String)c = new String();

But as I mentioned, i haven't test it situations like this.
I can think this further when i have more time ;)

-Vepo
 
Hi Vepo,

I think I'm gonna use the System.out.println(&quot;reply...) method for now.

Thanks for your help!

Cya
Outliner:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top