How to call method in jar file using terminal?

I have a compiled project in a jar file and I need to call a method from it. How can I do this in an ubuntu terminal?

+7
source share
4 answers

You cannot call any method from the terminal or cmd of any class.

You can execute the class through the terminal.

If your jar is executing, try: java -jar "Name of your Jar"

Or set the path to your bank: java -classpath path-to-jar <package>.<classname>

+9
source
 java -cp path/to/jar <package>.<classname> 

Example:

 java -cp test.jar org.dekz.HelloWorld 
+5
source

I need to call a method from it.

This is not a very specific operator. if you are interested in calling the main method of your main class, you can do

 java -jar path/to/yourjar/yourJar.jar 

if you want to call a method from this class from another class, you need to add this jar to your classpath, and then you can access other methods. but since you wrote from the terminal, I assume that the first approach is suitable for you.

+4
source

you can call a specific method from the jar file if and only if the developer has programmed the file to call it from the outside. You can use jar explorer to fine-tune each class inside the jar file

0
source

All Articles