File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling a java class Watch "Calling a java class New topic

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

Rhea Karnam

Joined: May 06, 2003
Posts: 21
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!
Jessica Sant

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:

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="" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
I agree. Here's the link:
subject: Calling a java class's main method from another java program
jQuery in Action, 3rd edition