9

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

shift66
  • 11,760
  • 13
  • 50
  • 83

4 Answers4

12

You can not call any method from terminal or cmd of any class.

You can execute class through terminal.

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

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

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • 2
    We can give the method name as parameter. If it is public, it will run. java -jar . It executes all the methods with the same name across all the packages in the jar. – Sashank Oct 27 '16 at 08:20
  • How doe you call the method though? – Demodave Jul 08 '21 at 21:41
7
java -cp path/to/jar <package>.<classname>

example:

java -cp test.jar org.dekz.HelloWorld
dekz
  • 805
  • 1
  • 8
  • 17
4

I need to call a method from it.

This is not very specific statement. if you are interested to call main method of your Main class then you can do

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

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

jmj
  • 237,923
  • 42
  • 401
  • 438
-1

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