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

12

166

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?

Sitton answered 22/2, 2012 at 12:57 Comment(4)
type "javac -help"Prudery
docs.oracle.com/javase/6/docs/technotes/tools/index.html#basicPrudery
hope this helps you..Peloquin
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.Bryna
A
147

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
Armlet answered 22/2, 2012 at 14:8 Comment(6)
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.Drumlin
I have used this command but when tried to access the class present in jar file, I am getting ClassNotFoundException.Elegancy
what does .: do? sorry if it is a dumb question, but it is not obvious to me.Nyctophobia
@kensen. Typo mistake. replace : (colon) with semicolon (;)Ericaericaceous
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.javaUtility
@Nyctophobia 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 ;Ungovernable
W
32

Please try on Linux

javac -cp jarfile source file 

EXAMPLE :-

javac  -cp .:/jars/* com/template/*.java
Windmill answered 29/8, 2013 at 11:14 Comment(3)
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.Bilberry
#27915704 please refere this ,it may solve your problemWindmill
.:./jars/* works when trying to point to a local project directory jar/.Hardpan
I
26

Syntax will work on windows dos command:

javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java
Insecurity answered 11/7, 2013 at 15:19 Comment(2)
After successful execution of above command how to run the java class file?Tharpe
java -cp first.jar:second.jar:third.jar MyFileComplect
B
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!

Bloody answered 29/3, 2018 at 6:59 Comment(3)
Thanks for also showing how to execute. What does the dot colon do again? It's current directory and file separator?Carnahan
Yes it's the current directory and a Unix file separator (on Windows it's a semicolon).Midgard
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?Vasos
E
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.

Embree answered 22/2, 2012 at 13:39 Comment(0)
A
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]
Aggi answered 3/3, 2017 at 10:19 Comment(1)
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: MyFileTharpe
S
6

Try This.

javac -cp .:jars/jar1:jars/jar2:jars/jar3 com/source/*.java
Shellfish answered 26/4, 2017 at 23:7 Comment(0)
A
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.

Acinaciform answered 23/5, 2020 at 8:46 Comment(1)
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.Cornemuse
P
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
Precede answered 25/4, 2018 at 13:30 Comment(2)
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.Affiance
You mentioned specifying dependencies at runtime as well as compile time but don't explain how to specify them at runtime.Tenedos
M
2

some times making following change works:

java -cp ".;%CLASSPATH%" classfilename 

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

Myra answered 17/7, 2018 at 8:14 Comment(0)
Z
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

Zucker answered 25/9, 2017 at 14:38 Comment(0)
K
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.

Komsomol answered 23/11, 2022 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.