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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to give a string to other javaprogram?

Status
Not open for further replies.

Outy

Programmer
Feb 7, 2001
16
BE
Hi,

I have to make an application that starts another java-program. When the supervising program recieves a string for example "testthisisatest", the first 4 characters indicate the program that should be started, in this case test.java. The other characters "thisisatest" have to be send to the called program and should be shown on the screen.

How do I call another java-program and how do I pass the string to the other program?

Thx

Outliner
 
Hi,

You can use the method substring() found in the String class. It would be something like

String temp = "thisisatest";
String className = temp.substring(0,3);
String passInParam = temp.substring(4,temp.length()-1);
//className would have its value as "this";
//passInParam would have its value as "isatest"

For the passing in of parameters, I am not very sure on how you can do it so maybe someone else can suggest that to you :)

Gd Luck,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Hi.

What you need now, if I understand, is to invoke the main method of the compiled Java program. I think you meant test.class rather than test.java.

The idea is to use reflection, test this code, it launches the main method of the class specified in className (do not forget to set the correct class path) and with arguments cmdArray:

{
String[] cmdArray = new String[3];
String className = "test.launch.Test";//For example

cmdArray[0] = "arg1";
cmdArray[1] = "arg2";
cmdArray[2] = "arg3";

try
{
Class[] mainMethodArgsClasses = {cmdArray.getClass ()};
Object[] mainArgs = {cmdArray};
Class claz = Class.forName (className);
claz.getMethod ("main", mainMethodArgsClasses).invoke(null, mainArgs);
}
catch (Exception e)
{ e.printStackTrace (); }
}

I hope this will help you. --
Globos
 
Hello,

Thank you LeonTang and Globos for the quick replies!

Greetz
Outliner
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top