Thursday, August 20, 2009

A handy batch script to compile Java programs

[Updated: Dec 22, 2010]
I've always had some trouble remembering the right commands to compile a large-ish Java project from the command line when you don't have Ant at your disposal.
So I thought I should write about it so others (including myself) might find it handy for future reference.
The general idea is to have this kind of directory structure (duh!):


The batch file. Make changes to match your directories.
The last 2 lines create a simple txt file of all your .java file names in the src folder. It then uses the javac command to compile them into the classes folder.
set basedir=c:\projects\myproject
del /Q %basedir%\classes

mkdir %basedir%\classes

set cp=%basedir%\lib\XXX.jar;%basedir%\lib\YYY.jar;%CLASSPATH%

set JAVA_HOME=C:\Programs1\jdk1.6.0


dir /b /s %basedir%\src\*.java > %basedir%\java_files_to_compile.txt

%JAVA_HOME%\bin\javac -classpath %cp% -d %basedir%\classes @%basedir%\java_files_to_compile.txt

xcopy /S %basedir%\src\*.properties %basedir%\classes

jar -cvfm %basedir%\myproject-lib.jar %basedir%\ -C %basedir%\classes/ .


Ashwin Jayaprakash said...

Useful -