Advertisement
Advertisement


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


Question

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?

2014/08/07
1
121
8/7/2014 8:50:12 PM


Please try on Linux

javac -cp jarfile source file 

EXAMPLE :-

javac  -cp .:/jars/* com/template/*.java
2013/08/29

Syntax will work on windows dos command:

javac -cp ".;first.jar;second.jar;third.jar" MyJavaFile.java
2013/07/11

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.

2016/03/22

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!

2018/03/29

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]
2017/03/03

Source: https://stackoverflow.com/questions/9395207
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]