Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic