4

I want to execute specific method inside class in jar file which is don't have main method, with java command I tried java -cp classes.jar com.example.test.Application but I get this error Error: Could not find or load main class After decompile jar file I have found Application class inside it is there any way to call a static function inside Application class in a jar file?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Daniel.V
  • 2,322
  • 7
  • 28
  • 58

1 Answers1

6

You could use Jshell:

$ jshell --class-path  ~/.m2/repository/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar
|  Welcome to JShell -- Version 11.0.4
|  For an introduction type: /help intro

jshell> org.apache.commons.lang3.StringUtils.join("a", "b", "c")
$1 ==> "abc"

Or create a java class with main compile it and run with java:

Test.java:

class Test {
    public static void main(String[] args) {
        String result = org.apache.commons.lang3.StringUtils.join("a", "b", "c");
        System.out.println(result);
    }
}

Compile and run:

$ javac -cp /path/to/jar/commons-lang3-3.9.jar  Test.java
$ java -cp /path/to/jar/commons-lang3-3.9.jar:.  Test
abc
i.bondarenko
  • 3,442
  • 3
  • 11
  • 21