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

Calling main method of one program from another program

 
rajshkhr pandey
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajshkhr Pandey,
Do you mean in following example, when you run Test2.main() it is not printing anything?


It works for me
 
Campbell Ritchie
Sheriff
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic