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

357,796

Solution 1

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

Please try on Linux

javac -cp jarfile source file 

EXAMPLE :-

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

Solution 3

Syntax will work on windows dos command:

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

Solution 4

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 .:<file_1_name>.jar:<file_2_name>.jar <prog_name>.java
    
  3. To execute,

    java -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>
    

I hope this helps!

Solution 5

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.

Share:
357,796
user1177567
Author by

user1177567

Updated on December 27, 2020

Comments

  • user1177567
    user1177567 over 3 years

    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?

  • Arturo don Juan
    Arturo don Juan over 8 years
    When I try the EXAMPLE :-, with .:/jars replaced with the directory in which my JAR files are located, I get the error message javac: invalid flag: /location/of/first/jar/file.jar.
  • Jamsheer
    Jamsheer over 8 years
    stackoverflow.com/questions/27915204/… please refere this ,it may solve your problem
  • Sri
    Sri almost 8 years
    javac -classpath ".:/home/path/mail.jar;/home/path/servlet.jar" MyJavaFile.java worked for me. I was using mac. I read somewhere that ':' is used for unix.
  • Mohammed Rampurawala
    Mohammed Rampurawala almost 8 years
    I have used this command but when tried to access the class present in jar file, I am getting ClassNotFoundException.
  • Yash Agrawal
    Yash Agrawal about 7 years
    After successful execution of above command how to run the java class file?
  • Yash Agrawal
    Yash Agrawal about 7 years
    java command is showing error Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory I want to compile ** javac -classpath "/home/scorncer/Downloads/spark-core-2.3.jar" MyFile.java and **run java -cp /home/scorncer/Downloads/spark-core-2.3.jar: MyFile.java also i tried java -cp /home/scorncer/Downloads/spark-core-2.3.jar: MyFile
  • Mzq
    Mzq about 6 years
    what does .: do? sorry if it is a dumb question, but it is not obvious to me.
  • Krsna Chaitanya
    Krsna Chaitanya about 6 years
    java -cp first.jar:second.jar:third.jar MyFile
  • DaveyDaveDave
    DaveyDaveDave about 6 years
    This answer, while correct, doesn't really seem to add anything that the other answers don't already say. If there's some key difference between this and the other answers, it would be better to explain why your answer differs.
  • Pankaj Shinde
    Pankaj Shinde almost 6 years
    @kensen. Typo mistake. replace : (colon) with semicolon (;)
  • Michael Fulton
    Michael Fulton over 5 years
    You mentioned specifying dependencies at runtime as well as compile time but don't explain how to specify them at runtime.
  • ThisClark
    ThisClark over 5 years
    Thanks for also showing how to execute. What does the dot colon do again? It's current directory and file separator?
  • MathuSum Mut
    MathuSum Mut over 4 years
    Yes it's the current directory and a Unix file separator (on Windows it's a semicolon).
  • Alfaz Jikani
    Alfaz Jikani over 4 years
    Above didn't worked for me on ubuntu. I tried without quotation mark and it worked. Ex. javac -cp .:/home/path/mail.jar:/home/path/servlet.jar; MyJavaFile.java
  • double-beep
    double-beep almost 4 years
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
  • jackw11111
    jackw11111 over 3 years
    .:./jars/* works when trying to point to a local project directory jar/.
  • AliK
    AliK almost 3 years
    Surely the purpose or javac -cp libs/xxx.jar program.java would be to build a final independent output file so you no longer have to use -cp when running because that would mean I would need to copy my libs folder to where ever I wish to run the app?