How to run a class from a jar with from command-line with classpath specified
Solution 1
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
Solution 2
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.
Bhushan
I am here to learn, and also to contribute my two cents. Working as a Software Engineer. Sun Certified Java Programmer for Java 5.0 . #SOreadytohelp
Updated on July 09, 2022Comments
-
Bhushan over 1 year
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 formyProjImport.jar
to run andcom.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 classTestImporter
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?
-
user1002601 over 12 yearsRight idea, add myProjImport.jar to the classpath. But use
;
separator on Windows. -
Karthik Ramachandran over 12 yearsopps in that case: 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
-
Bhushan over 12 yearsAfter adding the line "Class-Path mysql-connector-java-5.1.13-bin.jar", I got this error: "Invalid or corrupt jarfile myProjImport.jar"
-
Bhushan over 12 yearsThanks Karthik and ykaganovich, your solution works. Karthik, please make the correction which ykaganovich has suggested- ';' instead of ':' since this is for Windows platform.
-
drevicko over 10 yearsI don't want to be guilty of java bashing, but why? why? why? can I not specify extra class paths when running a jar??