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?
The answer is 'Yes'. We can have two or more static methods with the same name, but differences in input parameters. For example, consider the following Java program.
Static method does not mean it runs only once. static means it can be access outside method without instantiating an instance of the class.
@SangitGurung it's not. The methods are executed in separate activation frames.
But when we try to call Non static function i.e, TestMethod() inside static function it gives an error - “An object refernce is required for non-static field, member or Property 'Program. TestMethod()”. So we need to create an instance of the class to call the non-static method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With