Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Launch a new java app from a java program

Thread view: 
Yao Qi - 29 Jun 2007 17:30 GMT
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

Signature

Yao Qi <qiyaoltc@gmail.com>    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)

bencoe@gmail.com - 29 Jun 2007 17:35 GMT
> 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
[quoted text clipped - 12 lines]
>
>   -- 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.

-----
Ben
http://www.plink-search.com
Steve W. Jackson - 29 Jun 2007 17:54 GMT
> > 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
[quoted text clipped - 24 lines]
> Ben
> http://www.plink-search.com

It will *definitely* do the trick (we do it in a small number of places
in our own app).  But I would strongly discourage the use of that
specific exec method...the one accepting a String array will almost
always be safer.
Signature

Steve W. Jackson
Montgomery, Alabama

Roedy Green - 29 Jun 2007 22:23 GMT
>I want to write a java program A to launch another java app B.

see http://mindprod.com/jgloss/exec.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Yao Qi - 30 Jun 2007 01:41 GMT
>>I want to write a java program A to launch another java app B.
>
> see http://mindprod.com/jgloss/exec.html

Yeah, that is what I want to find.  Thanks.

Best Regards

Signature

Yao Qi <qiyaoltc@gmail.com>    GNU/Linux Developer
http://duewayqi.googlepages.com/

Andrew Thompson - 30 Jun 2007 08:17 GMT
>>>I want to write a java program A to launch another java app B.
>>
>> see http://mindprod.com/jgloss/exec.html
>
>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?

Signature

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

Arne Vajhøj - 01 Jul 2007 00:58 GMT
>>>> I want to write a java program A to launch another java app B.
>>> see http://mindprod.com/jgloss/exec.html
[quoted text clipped - 4 lines]
> 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
Roedy Green - 01 Jul 2007 16:58 GMT
>Reflection code snippet:
>
[quoted text clipped - 4 lines]
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Tom Hawtin - 01 Jul 2007 18:49 GMT
> 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
Roedy Green - 01 Jul 2007 19:20 GMT
On Sun, 01 Jul 2007 18:49:31 +0100, Tom Hawtin
<usenet@tackline.plus.com> wrote, quoted or indirectly quoted someone
who said :

>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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 01 Jul 2007 20:46 GMT
>> Reflection code snippet:
>>
[quoted text clipped - 15 lines]
>
> 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
Roedy Green - 02 Jul 2007 01:31 GMT
>App means no guarantee of interface and a static main method.

If it is written in Java it must have a main method. Other wise how
would you start it?
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Stefan Ram - 02 Jul 2007 02:07 GMT
>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.)
Roedy Green - 02 Jul 2007 07:17 GMT
>  With a static initializer?
>
>  (The JVM might still require a main method.)

Then it would just be a class, not an App.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 02 Jul 2007 02:27 GMT
>> App means no guarantee of interface and a static main method.
>
> If it is written in Java it must have a main method. Other wise how
> would you start it?

App means (no guarantee of interface) and (a static main method)

:-)

Arne
Roedy Green - 02 Jul 2007 07:18 GMT
>App means (no guarantee of interface) and (a static main method)

Application implies you can start it from the command line. That
implies a main method.

Otherwise you would call it as class or a library.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 03 Jul 2007 03:01 GMT
>> App means (no guarantee of interface) and (a static main method)
>
> Application implies you can start it from the command line. That
> implies a main method.

True.

But it does definitely not imply an interface with a non
static method which your solution required.

Arne
Roedy Green - 03 Jul 2007 14:20 GMT
>But it does definitely not imply an interface with a non
>static method which your solution required.

Yes, the Class.forName approach requires the you to implement some
common interface in your dynamic classes.  See
http://mindprod.com/jgloss/exec.html#EXECINGJAVA
for details.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 04 Jul 2007 01:41 GMT
>> But it does definitely not imply an interface with a non
>> static method which your solution required.
>
> Yes, the Class.forName approach requires the you to implement some
> common interface in your dynamic classes.

No it does not.

You can use reflection instead.

Arne
Roedy Green - 04 Jul 2007 03:12 GMT
>> Yes, the Class.forName approach requires the you to implement some
>> common interface in your dynamic classes.
>
>No it does not.
>
>You can use reflection instead.

The approach I document there does require an interface.  Reflection
is yet another approach.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Yao Qi - 01 Jul 2007 12:29 GMT
> Really?  exec seems a heavy handed way to start
> a new Java app. running.  Generally I would import
[quoted text clipped - 40 lines]
> 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

Signature

Yao Qi <qiyaoltc@gmail.com>    GNU/Linux Developer
http://duewayqi.googlepages.com/

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

Patricia Shanahan - 01 Jul 2007 14:59 GMT
...
> 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
Christian - 01 Jul 2007 14:08 GMT
Andrew Thompson schrieb:
>>>> I want to write a java program A to launch another java app B.
>>> see http://mindprod.com/jgloss/exec.html
[quoted text clipped - 4 lines]
> 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
Andrew Thompson - 02 Jul 2007 04:24 GMT
>Andrew Thompson schrieb:
>>>>> I want to write a java program A to launch another java app B.
...
>> 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...

Of course.

>...may be sharing
>the same singletons..

It is not suitable for all situations, and he OP has since(?)
clarified that this task needs to be done in a separate JVM.

And to the OP - do you now see the advantage of actually
*describing* the problem domain, rather than simply give a
vague description and 'what you want to do'?

Signature

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

Patricia Shanahan - 02 Jul 2007 04:49 GMT
>> Andrew Thompson schrieb:
>>>>>> I want to write a java program A to launch another java app B.
[quoted text clipped - 14 lines]
> *describing* the problem domain, rather than simply give a
> vague description and 'what you want to do'?

However, even with the initial description my assumption would be exec.
I don't think the main call technique should ever be used to "launch
another java app".

It is only really a good technique for running a main class that is
logically part of the calling application, so closely coordinated with
it that the caller and callee should run as one application, using a
single environment, class loader, heap, etc.

Patricia
Yao Qi - 02 Jul 2007 12:56 GMT
> It is not suitable for all situations, and he OP has since(?)
> clarified that this task needs to be done in a separate JVM.
>
> And to the OP - do you now see the advantage of actually
> *describing* the problem domain, rather than simply give a
> vague description and 'what you want to do'?

Is OP me here?  I think I described my question clearly.  Here is what I
posted here in this thread,

"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."

Is the description vague?  If there is something confusing, point it
out.  I would like to improve my English skills. :)

Best Regards

Signature

Yao Qi <qiyaoltc@gmail.com>    GNU/Linux Developer
http://duewayqi.googlepages.com/

Andrew Thompson - 02 Jul 2007 15:55 GMT
>> It is not suitable for all situations, and he OP has since(?)
>> clarified that this task needs to be done in a separate JVM.
[quoted text clipped - 4 lines]
>
>Is OP me here?  

Yes.  OP stands for the 'Original Poster' - the person
that started the thread.

>...I think I described my question clearly.  Here is what I
>posted here in this thread,
[quoted text clipped - 6 lines]
>
>Is the description vague?  

No.

>...If there is something confusing, point it
>out.  I would like to improve my English skills. :)

Your english skills are fine.  Apparently a lot better than
mine on the day I misread the question and made that
remark, ..or it's been a long thread and I missed some
details, or..(insert other pathetic excuses here)   ;-)

One thing I did notice in my browsings of this thread and
*links*, is that ProcessBuilder seems to be the preferred
way to go about getting a Process, rather than calling
exec().  

Has that been explicitly mentioned/discussed?

Signature

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

Roedy Green - 01 Jul 2007 17:25 GMT
>Really?  exec seems a heavy handed way to start
>a new Java app. running.

Exec is usually used only for starting a non-java app.

see  http://mindprod.com/jgloss/exec.html#EXECINGJAVA
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Patricia Shanahan - 01 Jul 2007 17:47 GMT
>> Really?  exec seems a heavy handed way to start
>> a new Java app. running.
>
> Exec is usually used only for starting a non-java app.
>
> see  http://mindprod.com/jgloss/exec.html#EXECINGJAVA

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.

Patricia
Roedy Green - 01 Jul 2007 20:08 GMT
>> see  http://mindprod.com/jgloss/exec.html#EXECINGJAVA
>
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Arne Vajhøj - 01 Jul 2007 20:47 GMT
>> Really?  exec seems a heavy handed way to start
>> a new Java app. running.
>
> Exec is usually used only for starting a non-java app.

But it could start java.exe ...

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.