This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Java in General and the fly likes Calling main method of one program from another program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling main method of one program from another program" Watch "Calling main method of one program from another program" New topic
Author

Calling main method of one program from another program

rajshkhr pandey
Ranch Hand

Joined: Oct 14, 2009
Posts: 35
Hello to all,

I have one program which uses command line arguments for doing particular work. But when I am calling the main function from another program with appropriate arguments into string array, it does not work as it is working and giving output from the command line.

I have also tried "Runtime.exec()" method to call the class having main method, but results none.

Is there another way to perform the this opration?

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43418
    
  32
Don't understand the problem. Do you have all your classes in the same classpath? How are you invoking main() or Runtime.exec()?

Please supply more details and don't simply say "doesn't work".

Too difficult a question for "beginning". Moving thread.
Chandrakant Talele
Greenhorn

Joined: Dec 08, 2008
Posts: 4
Hi Rajshkhr Pandey,
Do you mean in following example, when you run Test2.main() it is not printing anything?


It works for me


~ Chandrakant Talele
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43418
    
  32
Welcome to the Ranch

That is probably not what the question is about, I am afraid, since that will obviously work readily.
Chandrakant Talele
Greenhorn

Joined: Dec 08, 2008
Posts: 4
Oops , yes I interpreted the problem incorrectly, my apologies
Here is the sample program that will work. I hope I am correct this time.
I am running it in eclipse and "bin" is where my .class files are.



Thanks Campbell Ritchie for the welcome
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42956
    
  73
Probably all problems that can occur using Runtime.exec are explained in this article.

If you can alter the code for both applications, the easiest would probably be to add an appropriate constructor to the app being called through which you can pass the parameters, and then to instantiate (and run) it directly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling main method of one program from another program
 
It's not a secret anymore!