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
  • 156,572
  • 25
  • 250
  • 300
Daniel.V
  • 2,414
  • 6
  • 23
  • 54

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,102
  • 3
  • 8
  • 18