How to include jar files with java file and compile in command prompt
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,
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).
To compile,
javac -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>.java
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:
- Open command prompt.
- Change directory to the location of you java file that you would like compile.
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;
Now, you may compile your java file. (command:
javac YourJavaFile.java
)
Hope this will resolve your dependency issue.
user1177567
Updated on December 27, 2020Comments
-
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 over 8 yearsWhen I try the EXAMPLE :-, with
.:/jars
replaced with the directory in which my JAR files are located, I get the error messagejavac: invalid flag: /location/of/first/jar/file.jar
. -
Jamsheer over 8 yearsstackoverflow.com/questions/27915204/… please refere this ,it may solve your problem
-
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 almost 8 yearsI have used this command but when tried to access the class present in jar file, I am getting ClassNotFoundException.
-
Yash Agrawal about 7 yearsAfter successful execution of above command how to run the java class file?
-
Yash Agrawal about 7 yearsjava 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 **runjava -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 about 6 yearswhat does
.:
do? sorry if it is a dumb question, but it is not obvious to me. -
Krsna Chaitanya about 6 yearsjava -cp first.jar:second.jar:third.jar MyFile
-
DaveyDaveDave about 6 yearsThis 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 almost 6 years@kensen. Typo mistake. replace : (colon) with semicolon (;)
-
Michael Fulton over 5 yearsYou mentioned specifying dependencies at runtime as well as compile time but don't explain how to specify them at runtime.
-
ThisClark over 5 yearsThanks for also showing how to execute. What does the dot colon do again? It's current directory and file separator?
-
MathuSum Mut over 4 yearsYes it's the current directory and a Unix file separator (on Windows it's a semicolon).
-
Alfaz Jikani over 4 yearsAbove 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 almost 4 yearsWhile 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 over 3 years
.:./jars/*
works when trying to point to a local project directoryjar/
. -
AliK almost 3 yearsSurely 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?