How to include jar files with java file and compile in command prompt

JavaJarCommand Prompt

Java Problem Overview


I have 3 jar files and a .java file that depends on these jar files. How do I compile the .java file with these jar files using a command prompt?

Java Solutions


Solution 1 - Java

You can include your jar files in the "javac" command using the "-cp" option.

javac -cp ".:/home/path/mail.jar:/home/path/servlet.jar;" MyJavaFile.java

Instead of "-cp" you could also use "-classpath"

javac -classpath ".:/home/path/mail.jar:/home/path/servlet.jar:" MyJavaFile.java

You could including the jars every time you compile by setting the environment variable "CLASSPATH" correctly. The environment variable will store the path where the jars and classes that needs to be used for compiling/executing any java file. You will not have to include the jars individually every time you compile you file.

Different machines have different methods to set the classpath as an environment variable. The commands for Windows, Linux, etc are different.

You can find more details in this blog.

http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html

Solution 2 - Java

Please try on Linux

javac -cp jarfile source file 

EXAMPLE :-

javac  -cp .:/jars/* com/template/*.java

Solution 3 - Java

Syntax will work on windows dos command:

javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java

Solution 4 - Java

The followings are steps,

  1. Copy all jars and your .java file in a same folder (It will be easy to mention file names instead of mentioning long path. Though you can keep jar and .java in separate folders).

  2. To compile,

    > javac -cp .:.jar:.jar .java

  3. To execute,

    > java -cp .:.jar:.jar

I hope this helps!

Solution 5 - Java

Try to add all dependency jar files to your class path through environment variable settings or use the below steps:

  1. Open command prompt.

  2. Change directory to the location of you java file that you would like compile.

  3. Set the classpath for your dependency jar files as shown below:

    set classpath=C:\Users\sarath_sivan\Desktop\jars\servlet-api.jar; C:\Users\sarath_sivan\Desktop\jars\spring-jdbc-3.0.2.RELEASE; C:\Users\sarath_sivan\Desktop\jars\spring-aop-3.0.2.RELEASE;

  4. Now, you may compile your java file. (command: javac YourJavaFile.java)

Hope this will resolve your dependency issue.

Solution 6 - Java

This will create .class file:

javac -classpath "[jarname with specified path]" [java filename]

This will execute class file:

java -cp [jarname with specified path]: [java filename]

Solution 7 - Java

Try This.

javac -cp .:jars/jar1:jars/jar2:jars/jar3 com/source/*.java

Solution 8 - Java

You need to specify the dependencies in compile time as well as runtime

To compile use this format

javac -cp "*.jar;classfile_path" filename.java

Example:

javac -cp "ojdbc6.jar;c:\programs" Main.java

Solution 9 - Java

some times making following change works:

java -cp ".;%CLASSPATH%" classfilename 

Note: ON Windows. For linux use $CLASSPATH instead.

Solution 10 - Java

javac -cp jars/jar1:jars/jar2:jars/jar3 abc.java

With -cp command we specify the path where to find the additional libraries which are required to compile the class. jar1, jar2 and jar3, available in jars folder are used to compile abc.java class.

Solution 11 - Java

If you are using Ubuntu:

/opt/JavaServices/sqlite $ export CLASSPATH=/opt/JarFiles/XXXX.jar:/opt/JarFiles/XXXX.jar:/opt/JavaServices/;javac SQLiteSample.java

Go to folder location (Out of package structure)

/opt/JavaServices $ export CLASSPATH=/opt/JarFiles/XXXXX.jar:/opt/JarFiles/XXXXX.jar:/opt/JavaServices/;java sqlite.SQLiteSample

Note: Please see the file locations and package names

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1177567View Question on Stackoverflow
Solution 1 - Javakensen johnView Answer on Stackoverflow
Solution 2 - JavaJamsheerView Answer on Stackoverflow
Solution 3 - Javauser2573056View Answer on Stackoverflow
Solution 4 - JavaimbondView Answer on Stackoverflow
Solution 5 - Java1218985View Answer on Stackoverflow
Solution 6 - JavaIsaqView Answer on Stackoverflow
Solution 7 - JavaShravyaView Answer on Stackoverflow
Solution 8 - JavaArockiaRajView Answer on Stackoverflow
Solution 9 - JavaGuruView Answer on Stackoverflow
Solution 10 - JavapratapView Answer on Stackoverflow
Solution 11 - JavasrihariView Answer on Stackoverflow