161

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?

user1177567
  • 1,663
  • 2
  • 12
  • 9

12 Answers12

145

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
Manoj
  • 853
  • 1
  • 7
  • 28
kensen john
  • 5,439
  • 5
  • 28
  • 36
  • 9
    `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. – Sri May 25 '16 at 05:01
  • 5
    I have used this command but when tried to access the class present in jar file, I am getting ClassNotFoundException. – Mohammed Rampurawala May 25 '16 at 17:39
  • 9
    what does `.:` do? sorry if it is a dumb question, but it is not obvious to me. – Mzq Mar 20 '18 at 10:10
  • 1
    @kensen. Typo mistake. replace : (colon) with semicolon (;) – Pankaj Shinde May 18 '18 at 08:01
  • 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` – Alfaz Jikani Sep 14 '19 at 11:02
  • @Mzq the `.` mean your current directory, you can replace it with your current full directory path. `:` just a separator for Unix-like OSes, on Windows, it should be `;` – sanme98 Dec 28 '22 at 01:01
32

Please try on Linux

javac -cp jarfile source file 

EXAMPLE :-

javac  -cp .:/jars/* com/template/*.java
Spudley
  • 166,037
  • 39
  • 233
  • 307
Jamsheer
  • 3,673
  • 3
  • 29
  • 57
  • 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`. – Arturo don Juan Jan 07 '16 at 23:55
  • http://stackoverflow.com/questions/27915204/javac-invalid-flag-activation-1-1-jar please refere this ,it may solve your problem – Jamsheer Jan 08 '16 at 03:42
  • `.:./jars/*` works when trying to point to a local project directory `jar/`. – jackw11111 Nov 08 '20 at 02:07
26

Syntax will work on windows dos command:

javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java
user2573056
  • 269
  • 3
  • 2
24

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!

imbond
  • 2,030
  • 1
  • 20
  • 22
  • 4
    Thanks for also showing how to execute. What does the dot colon do again? It's current directory and file separator? – ThisClark Jan 08 '19 at 14:41
  • 2
    Yes it's the current directory and a Unix file separator (on Windows it's a semicolon). – MathuSum Mut Aug 19 '19 at 14:41
  • 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? – AliK Jul 01 '21 at 23:49
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.

SANN3
  • 9,459
  • 6
  • 61
  • 97
1218985
  • 7,531
  • 2
  • 25
  • 31
9

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]
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Isaq
  • 91
  • 1
  • 1
  • 2
    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` – Yash Agrawal Mar 16 '17 at 14:47
6

Try This.

javac -cp .:jars/jar1:jars/jar2:jars/jar3 com/source/*.java
Shravya
  • 208
  • 3
  • 11
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.

pratap
  • 538
  • 1
  • 5
  • 11
  • 5
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) 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. – double-beep May 23 '20 at 08:47
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
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
ArockiaRaj
  • 588
  • 4
  • 17
  • 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. – DaveyDaveDave Apr 25 '18 at 13:52
  • 1
    You mentioned specifying dependencies at runtime as well as compile time but don't explain how to specify them at runtime. – Michael Fulton Aug 03 '18 at 16:37
2

some times making following change works:

java -cp ".;%CLASSPATH%" classfilename 

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

Guru
  • 2,739
  • 1
  • 25
  • 27
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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
srihari
  • 397
  • 2
  • 7
  • 18
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.

Rory
  • 159
  • 1
  • 8