如何使用指定类路径的命令行从 jar 运行类
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
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
指定 -jar 选项时,将忽略任何其他类路径选项。所以这不起作用:
但是如果您将 foo.jar 名称放入 MyJar.jar 内的 META-INF/manifest.mf 中:
那么 foo.jar 将在与 MyJar.jar 相同的级别上进行搜索,即在同一目录中。
有时我只是解压所有依赖的 JAR 并将其内容打包到 MyJar.jar 中。这样可以减少依赖。
When -jar option is specified, any other class path options are ignored. So this won't work:
But if you place foo.jar name into META-INF/manifest.mf within MyJar.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.