Search code examples
javajarjavacred5

How to include jar file when compiling java files using a batch file


I use this now without any jars and it works fine and compiles all the java files.

"C:\Program Files\Java\jdk1.7.0_21\bin\javac" org/redfire/screen/*.java -deprecation -classpath . pause

I want to know how to include jar files to this because some files need the servlet-api.jar. I tried

" -cp servlet-api.jar C:\Program Files\Java\jdk1.7.0_21\bin\javac" org/redfire/screen/*.java -deprecation -classpath . pause, but I get

The filename, directory name or volume label syntax is incorrect. in cmd.


Solution

  • You can try this:

    "C:\Program Files\Java\jdk1.7.0_21\bin\javac" -deprecation -cp .;servlet-api.jar -d . org\redfire\screen\*.java

    Note: Assusing you have servlet-api.jar in current directory and all java files in org\redfire\screen directory from current location from where you are executing this command/batch-file

    A better way to write in batch file is:

    set JAVA_COMPILER="C:\Program Files\Java\jdk1.7.0_21\bin\javac"
    set CLASSPATH=.;servlet-api.jar
    
    %JAVA_COMPILER%  -deprecation -cp %CLASSPATH% -d . org\redfire\screen\*.java 
    pause