简体   繁体   English

如何使用外部库运行 jar

[英]How to run a jar with external libs

First, I'm working in a linux environment.首先,我在 linux 环境中工作。 Second, I've got one jar file which contains all of my project's classes, including the main, in one directory.其次,我有一个 jar 文件,它在一个目录中包含我项目的所有类,包括主类。 Third, I've got all of the project's external dependent jars in a sub-directory called lib.第三,我在名为 lib 的子目录中获得了项目的所有外部依赖 jar。

The question is how do I run my program?问题是我如何运行我的程序? I've tried:我试过了:

java -classpath ".:/lib/*"  com.pom.ticketprocessing.main.TicketProcessing
java -classpath ".:/lib/*"  tp.com.pom.ticketprocessing.main.TicketProcessing
java -classpath ".:/lib/*"  tp.jar.com.pom.ticketprocessing.main.TicketProcessing
java -classpath ".:/lib/*"  TicketProcessing
java -classpath "tp.jar:/lib/*.jar"  TicketProcessing
java -classpath "./tp.jar:./lib/*.jar"  TicketProcessing

In each case I get the error: Error: Could not find or load main class TicketProcessing在每种情况下,我都会收到错误消息:错误:无法找到或加载主类 TicketProcessing

So, how do I run this program?那么,我该如何运行这个程序呢? Thanks in advance.提前致谢。

1) Since your own jar is not int the lib directory, you need both of them in classpath. 1) 由于您自己的 jar 不在lib目录中,因此您在类路径中需要它们。 That's why lines 1 - 4 are not correct.这就是第 1 - 4 行不正确的原因。 They are all missing tp.jar .他们都缺少tp.jar

2) To refer jars, classpath should either use names of the jars without any wildcards, or use directory following one asterisk. 2) 要引用 jar,classpath 应该使用不带任何通配符的 jar 名称,或者使用一个星号后的目录。 It means you can use .../lib/abc.jar:.../lib/def.jar , or .../lib/* , but following will be just ignored: .../lib/*.jar .这意味着您可以使用.../lib/abc.jar:.../lib/def.jar.../lib/* ,但以下将被忽略: .../lib/*.jar .

3) Pay attention to using of / . 3) 注意使用/ When path element begins with / , it means path in the file system root, not in the current directory.当 path 元素以/开头时,表示文件系统根目录中的路径,而不是当前目录中的路径。

4) Usage of . 4) 的用法 is important only when calling scripts or executable binaries in the current directory.仅在调用当前目录中的脚本或可执行二进制文件时才重要。 But when you provide path as an argument to Java, it is not necessary.但是当您将路径作为参数提供给 Java 时,就没有必要了。

In your case you need following command:在您的情况下,您需要以下命令:

java -classpath "tp.jar:lib/*" com.pom.ticketprocessing.main.TicketProcessing

Or if you like dots:或者,如果您喜欢点:

java -classpath "./tp.jar:./lib/*" com.pom.ticketprocessing.main.TicketProcessing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM