How to Include Jar Files with Java File and Compile in Command Prompt

How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?

The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):

java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp

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.

Compile java file with two jars from command line?

try this

javac -cp name1.jar:name2.jar code.java

note that if you are in Windows path separator should be ;

compile and run java with jar files

You need to specify the classpath when running your code, by using the same -cp args that you used when compiling, plus the folder where your compiled class is in.

In your cas that would mean something like java -cp .:jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner

The libs you specified are not included in the .class files generated, so Java still needs them to understand how to call the code that's not coming from your 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

Is it possible to add dependency class binaries to javac as a argument.(compiling a java file directly from terminal)

It is not possible to explicitly add binaries (i.e. ".class" files) as dependencies by passing them as javac command line arguments.

The javac command (and other Java toolchain commands) use a classpath to find dependency classes. This is a sequence of classpath entries which are either directories or JAR or ZIP files. So, to add a single dependency class, you would either need to put it into a directory on the classpath, or add it to a JAR or ZIP file on the classpath, or add a new entry (as above) to the classpath.

In your case, you need to add the JAR file for the Apache Commons Lang library to the classpath. If you are compiling like this:

 $ javac myjava.java

change it to this:

 $ javac -classpath .:<path/to/dependency.jar> myjava.java

For more information, you should read about how the classpath works and how it is specified. See

  • Setting the Class Path (Windows version).

  • Setting the Class Path (UNIX version). This applies to MacOS too.

Finally, note that explicitly downloading dependencies and adding them your build and runtime classpath is clunky and old-fashioned. Build tools like Maven and Gradle take care of it for you. If you plan to implement anything non-trivial in Java, you should learn to use at least one of these build tools.



Related Topics



Leave a reply



Submit