[英]How to call public static String method from jar from command-line?
We have a project (say, Foo ) which has some public static
methods, some of them return String
. 我们有一个项目(比方说, Foo )有一些
public static
方法,其中一些返回String
。 Now, I need to access that project's jar
from command-line and call one of those public static String
methods to get that String. 现在,我需要从命令行访问该项目的
jar
并调用其中一个public static String
方法来获取该String。
EDIT 1: 编辑1:
I need to pass that returned string as an argument to a shell script. 我需要将返回的字符串作为参数传递给shell脚本。 Something like this:
像这样的东西:
./my_bash_script.sh <value returned from jar> <more arguments>
How do we do it? 我们该怎么做呢?
Tried to call the jar from command-line, but that didn't help. 试图从命令行调用jar,但这没有帮助。
java -cp Foo.jar full.package.name.ClassName
Error: Could not find or load main class jar
It was expected, since the class doesn't have a Main function. 这是预期的,因为该课程没有主要功能。
Also tried to add a wrapper around the Foo.jar . 还试图在Foo.jar周围添加一个包装器。 Like, added this jar (Foo.jar) in my other project as a dependency, and called
public static String
method from Foo.jar
. 比如,将这个jar(Foo.jar)作为依赖项添加到我的其他项目中,并从
Foo.jar
调用public static String
方法。 Well, this works, but I have to return a String outside Java application and Main has to be void
. 嗯,这有效,但我必须在Java应用程序外返回一个String,而Main必须是
void
。
Can anyone please help me with this? 有人可以帮我这个吗?
Please ask for more explanation, if I have left out any detail, before down-voting. 如果我在投票前遗漏了任何细节,请询问更多解释。
You just need to create a main method that passes a command line parameter to the method and then does a System.out.println(result)
. 您只需要创建一个main方法,将命令行参数传递给该方法,然后执行
System.out.println(result)
。
A more general solution would require a wrapper that used reflection to invoke the right method and print out the result. 更通用的解决方案需要一个使用反射来调用正确方法并打印出结果的包装器。
You could do that via something like this: 你可以通过以下方式做到这一点:
public class InvokeMethod {
public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
String clazz = args[0];
String method = args[1];
String[] methodArgs = new String[args.length - 2];
Class<?>[] methodParam = new Class[args.length - 2];
for (int i = 2; i<args.length; i++) {
methodArgs[i-2] = args[i];
methodParam[i-2] = String.class;
}
Class<?> c = Class.forName(clazz);
Method m = c.getMethod(method, methodParam);
String result = (String) m.invoke(methodParam);
System.out.println(result);
}
}
Compile that and package it into invoke.jar and then you can just do java -cp invoke.jar:your_real_jar.jar InvokeMethod some.class.Here method_name arg1 arg2 arg3...
编译并将其打包到invoke.jar然后你就可以执行
java -cp invoke.jar:your_real_jar.jar InvokeMethod some.class.Here method_name arg1 arg2 arg3...
the easiest solution would be : create & compile a new java console application as JAR, include your other JAR and 最简单的解决方案是:创建和编译一个新的Java控制台应用程序作为JAR,包括你的其他JAR和
System.out.println
the output from it, easy as cake. 它的输出,很容易像蛋糕。 After that you can start your new JAR with
之后,您可以开始使用新的JAR
java -jar FILE.jar
and have the output just the way you need it 并按照您需要的方式输出
You can pass the output of the wrapper to your shell script. 您可以将包装器的输出传递给shell脚本。
Java Java的
// -- Test.java ----
public class Test
{
public static void main(String args[])
{
// Call the method from Foo.jar here
System.out.println("Hello" + " World");
}
}
Shell 贝壳
# ----- test.sh ------
#!/bin/sh
grep "$1" input.txt
input.txt input.txt中
# Sample Input
Line 1 : Hello World
Line 2 : Here is Hello World Again
Line 3 : This is not Hello + World
Line 4 : AB Hello World CD
Commands 命令
javac Test.java
./test.sh "`java Test`" # Note the `
Sample o/p 样本o / p
Line 1 : Hello World
Line 2 : Here is Hello World Again
Line 4 : AB Hello World CD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.