168

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?

4
  • 10
    type "javac -help" Commented Feb 22, 2012 at 12:59
  • 2
    docs.oracle.com/javase/6/docs/technotes/tools/index.html#basic Commented Feb 22, 2012 at 13:00
  • 2
    hope this helps you.. Commented Feb 22, 2012 at 13:07
  • 2
    Note to self: you must use -cp/-classpath flag before the name of the java file you want to run, otherwise it will ignore the flag. java -cp ".;magic.jar" Foo is ok java Foo -cp ".;magic.jar"is not. Commented Dec 18, 2016 at 17:52

12 Answers 12

148

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
I have used this command but when tried to access the class present in jar file, I am getting ClassNotFoundException.
what does .: do? sorry if it is a dumb question, but it is not obvious to me.
@kensen. Typo mistake. replace : (colon) with semicolon (;)
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
|
34

Please try on Linux

javac -cp jarfile source file 

EXAMPLE :-

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

3 Comments

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.
stackoverflow.com/questions/27915204/… please refere this ,it may solve your problem
.:./jars/* works when trying to point to a local project directory jar/.
27

Syntax will work on windows dos command:

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

2 Comments

After successful execution of above command how to run the java class file?
java -cp first.jar:second.jar:third.jar MyFile
26

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!

3 Comments

Thanks for also showing how to execute. What does the dot colon do again? It's current directory and file separator?
Yes it's the current directory and a Unix file separator (on Windows it's a semicolon).
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?
17

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.

Comments

10

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]

1 Comment

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
6

Try This.

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

Comments

4

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.

1 Comment

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.
3

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

2 Comments

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.
You mentioned specifying dependencies at runtime as well as compile time but don't explain how to specify them at runtime.
2

some times making following change works:

java -cp ".;%CLASSPATH%" classfilename 

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

Comments

0

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

Comments

0

Plenty of these answers helped me, but none that were exactly what I needed.

Assumptions:

  1. Windows OS

  2. JAR file and java file are in same directory

javac -cp <jar filename>.jar <filename>.java

java -cp <jar filename>.jar; <filename>

Keep in mind the syntax needs to exactly match. Cannot exclude file extensions or the semi colon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.