aspose file tools*
The moose likes Java in General and the fly likes Calling a java class's main method from another java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling a java class Watch "Calling a java class New topic
Author

Calling a java class's main method from another java program

Rhea Karnam
Greenhorn

Joined: May 06, 2003
Posts: 21
Hi,
I need to call a java class's main method from my program, with certain parameters. Also, i need to get back the exit value/ status of this called java class.
How do I do this from within my JAva program? Any pointers? I tried using the Runtim.exec() method, but that doesn't recognize my parameters. I tried calling the JavaClass.main(Args), but I don't know how to check for the exit value, this way...
Help pls!
Rhea
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

the correct way to call another program's main method is just like you would call any other method: Program.main(String[]) -- just like you stated.

The problem is... as far as I see it you can't "capture" the status code that the main method gives.

Check out the API for System.exit(int) (pay particular attention to the part I bolded)
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

This method calls the exit method in class Runtime. This method never returns normally.

The call System.exit(n) is effectively equivalent to the call:

Runtime.getRuntime().exit(n)
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
You will have to use Runtime.exec to get the exit value of the child. What problems are you having with that? The program you run will need to be the JVM but otherwise it should work fine.


<a href="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a java class's main method from another java program