Java:如何从命令行导入 jar 文件 [英] Java: how to import a jar file from command line

查看:37
本文介绍了Java:如何从命令行导入 jar 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 从命令行 (Windows) 调用一个类(主方法).
该类导入其他类(其他 jars).

I'm trying to call a class (main method) from command line (Windows) with Java.
The class imports other classes (other jars).

我总是从我的主程序导入的类中得到找不到类的异常".

I always get "class not found exception" from a class that my main program imports.

这是我尝试过的:

  • 添加一个 CLASSPATH 环境.var 引用的库所在的路径(不工作)

  • Add a CLASSPATH env. var with the path where the referenced lib resides (not working)

我在从命令行调用java -jar myjar.jar"时尝试了所有这些不同的参数:-classpath lib/"、<代码>-classpath ./lib/", "-classpath lib", "-cp lib/*", "-cp lib/*", "-classpath lib/referenced-class.jar", "-classpath ./lib/referenced-class.jar" (lib 是引用的 jar 所在的位置)

I tried with all these different parameters when calling "java -jar myjar.jar" from command line : "-classpath lib/", "-classpath ./lib/", "-classpath lib", "-cp lib/*", "-cp lib/*", "-classpath lib/referenced-class.jar", "-classpath ./lib/referenced-class.jar" (lib is where the referenced jar resides)

我尝试将所有引用的 jar 打包到我的主类所在的 jar 中...

I tried packaging all the referenced jar inside my jar where my main class resides...

除此之外,我还尝试使用以下命令指定 Manifest 文件中的类:Class-path referenced-jar.jar 我也试过 Class-path lib/referenced-jar.jar

And with all that, I also tried to specify the classes inside the Manifest file with: Class-path referenced-jar.jar and I also tried Class-path lib/referenced-jar.jar

推荐答案

如果您碰巧知道要运行的主类的名称,则可以在没有 -jar 命令行参数的情况下运行它:

You could run it without the -jar command line argument if you happen to know the name of the main class you wish to run:

java -classpath .;myjar.jar;lib/referenced-class.jar my.package.MainClass

如果您使用的是 linux,则应该使用:"而不是;"在类路径中.

If perchance you are using linux, you should use ":" instead of ";" in the classpath.

这篇关于Java:如何从命令行导入 jar 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆