Is this possible to call a specific method (other than main) of a class in a jar file from command line?
            Asked
            
        
        
            Active
            
        
            Viewed 1.1k times
        
    2
            
            
        - 
                    4You question is very unclear. Please provide more details. The short answer is 'yes'. you can call from the jar everything you can call from main. :) – Mark Bramnik Aug 09 '12 at 09:08
 - 
                    You mean you want to run a jar file but invoke a method other than main? – DaveJohnston Aug 09 '12 at 09:11
 - 
                    Yeah, invoke class method from jar other than main using command-line – Yermek Makulbek Aug 09 '12 at 09:14
 - 
                    Your question is not just unclear but meaningless. You call methods from other methods, not from JAR files. The only method you don't call from another method is `main()`, that being what it's for. – user207421 Aug 09 '12 at 10:20
 
4 Answers
6
            If you are talking about running Java code from the command-line, then no.
You can specify a class name, but not which method to call, that always has to be public static void main(String[] argv).
What you could do is write a helper class (or script like BeanShell) to do that.
java -cp theJar.jar;.  my.helper.WrapperClass theClassToCall theMethodtoCall arg1 arg2
        Thilo
        
- 257,207
 - 101
 - 511
 - 656
 
4
            
            
        Put the Jar into you class path, instansiate the class in question & call the method?
        MadProgrammer
        
- 343,457
 - 22
 - 230
 - 366
 
2
            
            
        Yes, all you need to do is to create a class (with a main) which calls the method you want.  It doesn't have to be in the jar. It can be a plain .class file provided it and the jar are in the class path.
        Peter Lawrey
        
- 525,659
 - 79
 - 751
 - 1,130