Java command line with external .jar
Concatenate each jar file argument to cp with:
; on Windows
: on Linux or Mac
e.g.
java -cp <path>\TOOLS.jar;.;<path>\jar2.jar;<path>\jar3.jar HelloWorld
on newer JVMs (6+, I think) you can also use the * to append all JARs in a directory e.g.
java -cp .;<path>\*; HelloWorld
To go a step further and create a single packaged executable see this question.
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.
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:
- include all jar files from the
lib
directory into the manifest (you can use relative paths there) - Specify everything (including your jar) on the commandline using
-cp
:java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
How to run Java program in terminal with external library JAR
You can do :
1) javac -cp /path/to/jar/file Myprogram.java
2) java -cp .:/path/to/jar/file Myprogram
So, lets suppose your current working directory
in terminal is src/Report/
javac -cp src/external/myfile.jar Reporter.java
java -cp .:src/external/myfile.jar Reporter
Take a look here to setup Classpath
External jar files are not detected when compiled using cmd.?
use classpath (-cp)
javac -cp %YOUR_JAR_LOCATION% mail.java
Example :
javac -cp ".:./jars/mail.jar" helloworld.java
java -cp ".:./jars/mail.jar" helloworld
For Windows, ":"
should be replaced with ";"
and make sure your jar file path is correct.
Adding external dependencies to a jar file through the command line
You're not understanding what the classpath is. The classpath is a collection of jar files and directories where Java looks for classes (and other resources loaded by the class loader).
If a program uses the class com.foo.Bar
, what must be in the classpath is not the file /somedirectory/com/foo/Bar.class
. What must be in the classpath is the directory /somedirectory
. Or the jar file containing that class.
From that base directory or jar file, the class loader will then look for a file corresponding to the class name:
com.foo.Bar --> com/foo/Bar.class
This is essential, because it allows Java to have access to hundreds of classes at once, without having to list those hundreds of class files in the classpath. All you need in the classpath is the directory or jar containing those hundreds of classes.
Also note that /usr/lib/jvm/java-8-jdk/jre/lib/*
must not be inthe classpath either. Java knows where to find the libraries of the JRE itself.
Related Topics
Noclassdeffounderror While Trying to Run My Jar with Java.Exe -Jar...What's Wrong
Java: Get Month Integer from Date
How to Pass Parameter to Jsp:Include via C:Set? What Are the Scopes of the Variables in Jsp
How to Mock Java.Time.Localdate.Now()
Building an Uberjar with Gradle
How to Pause/Sleep/Wait in a Java Swing App
How to Add Button in a Row of Jtable in Swing Java
Try/Catch Versus Throws Exception
Java.Lang.Unsupportedclassversionerror Unsupported Major.Minor Version 51.0
Java Server with Multiclient Communication
Can't Rid of 'T' in Localdatetime
How to Select an Item from a Dropdown List Using Selenium Webdriver with Java
List All Files in the Folder and Also Sub Folders
How Do Format a Phone Number as a String in Java
Comparing Date Strings in Java
How to Dynamically Build a Multi-Dimensional Array in Java
Pdfbox - Signature Validity Checkmark Not Visible in Acrobat Reader