• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method from the command prompt  RSS feed

 
Venish Joe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to call a particular method from the command prompt while executing a java class.

That is, say I have a class with two methods, main() and test(). The class name can be TestClass. Is it possible to call the method test() while running the TestClass from the command prompt ?

Thanks
Venish Joe
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is not supported directly by the JVM. but of course you could write some code reading from command line and calling the corresponding method.

how many instances of your TestClass will there be ? is the test() method static ? if there are many instances and the method is not static then you need to keep a list of the instances and pick one (random, some algorithm, fifo, ...). if its static then there is nothing to pick.

you might want to check reflection which would allow you to find/invoke a method which name you typed on the command line.

hope this helps :-)

pascal
 
Venish Joe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks pascal betz for your prompt response.

There will be one instance of TestClass and the method test()
can be static.

So under this condition, which will be the best approach ?

Thanks
Venish Joe
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you just want to trigger execution of methods from command line or is there something else running in the background.
if there is something else, then you need to think about threads since otherwise your application would be blocked waiting for IO on command line (or use java.nio stuff).

depending on how many methods there are to be called and how dynamic you want to be... you can use either reflection or some predefined keywords;




perhaps you can come up with something and post again if you are stuck.

pascal
 
Venish Joe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again thanks, pascal betz.

I had come up with a solution, by using reflection. It will take the classname and the method name as a paramters and execute them.

Thanks
Venish Joe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!