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?
Asked
Active
Viewed 1,797 times
4

Dave Newton
- 158,873
- 26
- 254
- 302

Daniel.V
- 2,322
- 7
- 28
- 58
-
you cannot do this. write a test case. thats what tests are for isn't it? – pvpkiran Sep 30 '19 at 13:12
-
2create another class with a main method that calls your static method. – Maurice Perry Sep 30 '19 at 13:14
-
@MauricePerry this is jar file how can i create another class with main method? – Daniel.V Sep 30 '19 at 13:17
-
What is the ultimate goal? You create a new Java class that calls the method in question. – Dave Newton Sep 30 '19 at 13:19
-
1@Daniel.V just because you can't create a main method in that jar, doesn't mean you can't create a main methodµ – Stultuske Sep 30 '19 at 13:20
-
As other comments have said, this is not something you should do or want to do. You could write a main class which calls the static function in question reflectivly. An alternative which is really hacky and will cause initialization problems is to call your static function from a static initializer. – PiRocks Sep 30 '19 at 13:26
-
Are you using JavaFX by any chance? – PiRocks Sep 30 '19 at 13:26
-
@Daniel.V put your class in another jar file – Maurice Perry Sep 30 '19 at 13:30
1 Answers
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