Linux 如何使用终端调用jar文件中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7950538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:56:02  来源:igfitidea点击:

How to call method in jar file with terminal?

javalinuxjarterminal

提问by shift66

I have a compiled project in a jar file and I need to call a method from it.How can I do that in ubuntu terminal?

我在 jar 文件中有一个已编译的项目,我需要从中调用一个方法。如何在 ubuntu 终端中执行此操作?

采纳答案by Sumit Singh

You can not call any method from terminal or cmd of any class.

您不能从任何类的终端或 cmd 调用任何方法。

You can execute class through terminal.

您可以通过终端执行类。

If your jaris executable try: java -jar "Name of your Jar"

如果您jar是可执行的,请尝试: java -jar "Name of your Jar"

Or set the class path of your jar: java -classpath path-to-jar <package>.<classname>

或者设置 jar 的类路径: java -classpath path-to-jar <package>.<classname>

回答by dekz

java -cp path/to/jar <package>.<classname>

example:

例子:

java -cp test.jar org.dekz.HelloWorld

回答by Jigar Joshi

I need to call a method from it.

我需要从中调用一个方法。

This is not very specific statement. if you are interested to call main method of your Main class then you can do

这不是很具体的说法。如果您有兴趣调用 Main 类的 main 方法,那么您可以这样做

java -jar path/to/yourjar/yourJar.jar

if you want to call a method from this class from another class then you need to add this jar into your classpath and then you can access other methods. but since you wrote from terminal I assume the first approach fits for you

如果你想从另一个类调用这个类的方法,那么你需要将这个 jar 添加到你的类路径中,然后你就可以访问其他方法。但既然你是从终端写的,我认为第一种方法适合你

回答by Ajmal Muhammad P

you can call a specific method from a jar file if and only if the developer programmed the file to call it from outside. You can use a jar explorer to fine the details of each class inside a jar file

当且仅当开发人员对该文件进行编程以从外部调用它时,您才能从 jar 文件调用特定方法。您可以使用 jar 资源管理器来细化 jar 文件中每个类的详细信息