Java: How to Import a Jar File from Command Line

Java: how to import a jar file from command line

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

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

run java program from cmd with class imports from jar files

Since it was posted as a comment and I don't know if it would be visible for future reference I'm gonna write the solution down here as proposed by the user mlk

I fixed my java command to java -cp ".;jar/path/number/1";"jar/path/number/2" ParserDemo and it worked perfectly!

This is because the current folder (.) was not included in the classpath so Java could not see the ParserDemo.class file.

Run a JAR file from the command line and specify classpath

When you specify -jar then the -cp parameter will be ignored.

From the documentation:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

You have two options:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. Specify everything (including your jar) on the commandline using -cp:

    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

execute jar file in command line using third party jar

Something you can also try is to extract the jar and modify the MANIFEST.MF file to add the third party jars to Class-Path and zip the folder back and rename the extension to .jar.

Also you can refer the below for other ways to modify the manifest.

  • http://www.mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/
  • http://docs.oracle.com/javase/tutorial/deployment/jar/update.html

importing external jar files

Compiling from the command-line:

javac -cp path_to_jar1.jar:path_to_jar2.jar Example.java

Running:

java -cp .:path_to_jar1.jar:path_to_jar2.jar Example

For Windows, use ; as a path-separator (instead of :).

Compile java program in command line with external .jar

when you are executing below line, you are getting the below error right ?

javac -cp paillier.jar CoupleGen.java

Error: Could not find or load main class CoupleGen

That means compiler is looking for the CouplenGen.java in paillier.jar. Actually CoupleGen is outside of the jar file.



Related Topics



Leave a reply



Submit