如何使用指定类路径的命令行从 jar 运行类

发布于 2024-11-05 07:22:58 字数 1443 浏览 0 评论 0原文

我正在尝试从 JAR 运行一个类。这个类不是这个 jar 中唯一的主类。此外,它还需要许多其他 jar 文件,我将它们保存在与此 Jar 相同的目录中。我尝试过的命令如下:

(mydir是我所有jar所在的目录,使用Windows平台)

需要mysql-connector-java-5.1.13-bin.jar myProjImport.jar 运行,com.mycomp.myProj.importer.csv.TestImporter 是我尝试运行的类。 “C:\Documents and Settings\user\workspace\myProjImport\src\conf\datasource.properties”“C:\temp\apollo_claims_test.txt” 是命令TestImporter 类所需的行参数 这是我尝试过的:

mydir>java -cp C:\temp\test_myProj\mysql-connector-java-5.1.13-bin.jar;. myProjImport.jar com.mycomp.myProj.importer.csv.TestImporter "C:\Documents and Settings\user\workspace\myProjImport\src\conf\datasource.properties" "C:\temp\apollo_claims_test.txt"

这是错误:

Exception in thread "main" java.lang.NoClassDefFoundError: myProjImport/jar
Caused by: java.lang.ClassNotFoundException: myProjImport.jar
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: myProjImport.jar.  Program will exit.

有人可以告诉我应该运行什么确切的命令吗?

I am trying to run a class from a JAR. This class is NOT the only main class in this jar. Also, it requires number of other jar files, which I have kept in the same directory as this Jar. The commands I have tried are as follows:

(mydir is the directory in which all of my jars are located, using Windows platform)

mysql-connector-java-5.1.13-bin.jar is needed for myProjImport.jar to run and com.mycomp.myProj.importer.csv.TestImporter is the class i am trying to run.
"C:\Documents and Settings\user\workspace\myProjImport\src\conf\datasource.properties" and "C:\temp\apollo_claims_test.txt" are the command line arguments required by the class TestImporter
Here is what I have tried:

mydir>java -cp C:\temp\test_myProj\mysql-connector-java-5.1.13-bin.jar;. myProjImport.jar com.mycomp.myProj.importer.csv.TestImporter "C:\Documents and Settings\user\workspace\myProjImport\src\conf\datasource.properties" "C:\temp\apollo_claims_test.txt"

And here is the error:

Exception in thread "main" java.lang.NoClassDefFoundError: myProjImport/jar
Caused by: java.lang.ClassNotFoundException: myProjImport.jar
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: myProjImport.jar.  Program will exit.

Can someone please tell me what exact command should I run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-11-12 07:22:58

尝试:

java -cp C:\temp\test_myProj\mysql-connector-java-5.1.13-bin.jar;myProjImport.jar com.mycomp.myProj.importer.csv.TestImporter "C:\Documents and Settings \user\workspace\myProjImport\src\conf\datasource.properties" "C:\temp\apollo_claims_test.txt"

提供了您从与 myProjImport.jar 相同的目录运行此文件

try:

java -cp C:\temp\test_myProj\mysql-connector-java-5.1.13-bin.jar;myProjImport.jar com.mycomp.myProj.importer.csv.TestImporter "C:\Documents and Settings\user\workspace\myProjImport\src\conf\datasource.properties" "C:\temp\apollo_claims_test.txt"

provided your running this from the same direcotry as myProjImport.jar

笙痞 2024-11-12 07:22:58

指定 -jar 选项时,将忽略任何其他类路径选项。所以这不起作用:

java -jar MyJar.jar -classpath foo.jar

但是如果您将 foo.jar 名称放入 MyJar.jar 内的 META-INF/manifest.mf 中:

Class-Path: foo.jar

那么 foo.jar 将在与 MyJar.jar 相同的级别上进行搜索,即在同一目录中。

有时我只是解压所有依赖的 JAR 并将其内容打包到 MyJar.jar 中。这样可以减少依赖。

When -jar option is specified, any other class path options are ignored. So this won't work:

java -jar MyJar.jar -classpath foo.jar

But if you place foo.jar name into META-INF/manifest.mf within MyJar.jar:

Class-Path: foo.jar

Then the foo.jar will be searched on the same level as MyJar.jar, i.e. in the same directory.

Sometimes I just unpack all dependent JARs and pack their content into MyJar.jar. Fewer dependencies this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文