Launch a new java app from a java program

Y

Yao Qi

I want to write a java program A to launch another java app B. I hope I
could configure the argument of VM to launch java app B, just like what
Eclipse JDT does.(Eclipse is a java program, and it could launch other
java apps.) I do not want to create a new thread for java app B. I
prefer to run java app B on another process separately.

How could I do this?

Best Regards

--
Yao Qi <[email protected]> GNU/Linux Developer
http://duewayqi.googlepages.com/

"The world is beating a path to our door"

-- Bruce Perens, (Open Sources, 1999 O'Reilly and Associates)
 
B

bencoe

I want to write a java program A to launch another java app B. I hope I
could configure the argument of VM to launch java app B, just like what
Eclipse JDT does.(Eclipse is a java program, and it could launch other
java apps.) I do not want to create a new thread for java app B. I
prefer to run java app B on another process separately.

How could I do this?

Best Regards

--
Yao Qi <[email protected]> GNU/Linux Developerhttp://duewayqi.googlepages.com/

"The world is beating a path to our door"

-- Bruce Perens, (Open Sources, 1999 O'Reilly and Associates)

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String)

Runtime.exec("command");

will probably do the trick.
 
A

Andrew Thompson

Yao said:
Yeah, that is what I want to find.

Really? exec seems a heavy handed way to start
a new Java app. running. Generally I would import
the Java class and instantiate it normally, or call its
main with args if needed.

If the class is not known until runtime, then perhaps
reflection can do the trick..

<sscce>
class A {

String[] args = {
"a","b","c"
};

A() throws Exception {
Class bClass = this.getClass().forName("B");
B b = (B)bClass.newInstance();
b.main(args);
}

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

A a = new A();
}
}

class B {

public static void main(String[] args) {
System.out.println("args: ");
for (int ii=0; ii< args.length; ii++) {
System.out.println(args[ii]);
}
}
}
</sscce>

..though I am not yet convinced that either exec
or reflection is needed for this task.

Tell us - why can you not simply import class B
into your code?

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
G

Guest

Andrew said:
Really? exec seems a heavy handed way to start
a new Java app. running. Generally I would import
the Java class and instantiate it normally, or call its
main with args if needed.

I completely agree.
If the class is not known until runtime, then perhaps
reflection can do the trick..

Reflection code snippet:

Class declarg[] = new Class[1];
declarg[0] = String[].class;
Method m = Class.forName(clznam).getMethod("main", declarg);
Object callarg[] = new Object[1];
callarg[0] = args;
m.invoke(null, callarg);

Arne
 
Y

Yao Qi

Andrew Thompson said:
Really? exec seems a heavy handed way to start
a new Java app. running. Generally I would import
the Java class and instantiate it normally, or call its
main with args if needed.

If the class is not known until runtime, then perhaps
reflection can do the trick..

<sscce>
class A {

String[] args = {
"a","b","c"
};

A() throws Exception {
Class bClass = this.getClass().forName("B");
B b = (B)bClass.newInstance();
b.main(args);
}

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

A a = new A();
}
}

class B {

public static void main(String[] args) {
System.out.println("args: ");
for (int ii=0; ii< args.length; ii++) {
System.out.println(args[ii]);
}
}
}
</sscce>

.though I am not yet convinced that either exec
or reflection is needed for this task.

Tell us - why can you not simply import class B
into your code?

First let me introduce the program we are doing now. We are writing a
java tool to detect the runtime features of another java program. In
order to do this, we write a JVM TI agent to monitor some events in
JVM.

We want to invoke user's java program, with some special VM parameters,
such as -agentpath, in a new instance of VM by our java tool. That is
the reason why I do not simply import user's class in our tool, and
invoke the "main" method.

What we want is, in our java tool, we could invoke user's java program
on a new VM instance, with some special parameters to this VM.

Best Regards

--
Yao Qi <[email protected]> GNU/Linux Developer
http://duewayqi.googlepages.com/

Thank goodness modern convenience is a thing of the remote future.
-- Pogo, by Walt Kelly
 
C

Christian

Andrew said:
Really? exec seems a heavy handed way to start
a new Java app. running. Generally I would import
the Java class and instantiate it normally, or call its
main with args if needed.

but without exec the program will run in the same jvm... may be sharing
the same singletons..

crashing each other if one of it runs out of memory..

that may ususally not affect you but its kind of insecure..

Christian
 
P

Patricia Shanahan

Yao Qi wrote:
....
What we want is, in our java tool, we could invoke user's java program
on a new VM instance, with some special parameters to this VM.
....

In that case, exec is the way to go. I prefer ProcessBuilder to the
Runtime exec, because of the way the command and environment are specified.

Patricia
 
R

Roedy Green

Reflection code snippet:

Class declarg[] = new Class[1];
declarg[0] = String[].class;
Method m = Class.forName(clznam).getMethod("main", declarg);
Object callarg[] = new Object[1];
callarg[0] = args;
m.invoke(null, callarg);

Usually you can do something even simpler. Have all your dynamic
classes implement an interface. Then all you need is

Delegate d = (Delegate) ( Class
.forName( "com.mysite.mypackage."
+ name )
.newInstance() );
d.doSomething();

Where Delegate is your interface.
 
T

Tom Hawtin

Roedy said:
Delegate d = (Delegate) ( Class
.forName( "com.mysite.mypackage."
+ name )

If the class is one of a fixed set known at build time, you might as
well ignore reflection altogether.
.newInstance() );

Class.newInstance handles exceptions badly, so I would go through
Constructor instead.

Tom Hawtin
 
R

Roedy Green

If the class is one of a fixed set known at build time, you might as
well ignore reflection altogether.

you might get the list of possibilities from a run-time properties
file or by doing a File.list
 
R

Roedy Green

I've taken a look there, but did not see how to get a new JVM, with
its own environment, JVM parameters, static data, class loader etc.
running without exec.

have another look.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Roedy said:
Reflection code snippet:

Class declarg[] = new Class[1];
declarg[0] = String[].class;
Method m = Class.forName(clznam).getMethod("main", declarg);
Object callarg[] = new Object[1];
callarg[0] = args;
m.invoke(null, callarg);

Usually you can do something even simpler. Have all your dynamic
classes implement an interface. Then all you need is

Delegate d = (Delegate) ( Class
.forName( "com.mysite.mypackage."
+ name )
.newInstance() );
d.doSomething();

Where Delegate is your interface.

App means no guarantee of interface and a static main method.

I can not really see the relevance in the above.

Arne
 
S

Stefan Ram

Roedy Green said:
If it is written in Java it must have a main method.
Other wise how would you start it?

With a static initializer?

(The JVM might still require a main method.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top