如何使用命令行从指定了类路径的jar运行类 [英] How to run a class from a jar with from command-line with classpath specified
问题描述
我正在尝试从JAR运行一个类。这个类不是这个jar中唯一的主类。此外,它需要许多其他jar文件,我保存在与此Jar相同的目录中。我尝试过的命令如下:
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是我的所有罐子所在的目录,使用Windows平台)
(mydir is the directory in which all of my jars are located, using Windows platform)
运行和 myProjImport.jar需要mysql-connector-java-5.1.13-bin.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
这里是我试过的:
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"
这是错误:
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?
推荐答案
尝试:
java -cp C:\ temp \ test_myProj \ mysql-connector-java-5.1.13-bin.jar; myProjImport.jar com.mycomp.myProj.importer.csv.TestImporterC:\Documents and Settings\user\workspace\myProjImport\src \ conf\datasource.propertiesC:\ temp \ apollo_claims_test.txt
如果你从同一个地方运行它direcotry as myProjImport.jar
provided your running this from the same direcotry as myProjImport.jar
这篇关于如何使用命令行从指定了类路径的jar运行类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!