by Alexandre Alapetite on 2007-09-30; updated 2011-09-09

Java batch compile

It is sometimes necessary to compile a Java project on a machine without any development tool.

I propose on this page a batch script (.bat) for Windows (MS-DOS style) able to locate the best Java version available on the machine and to use it to automatically compile a full Java project.


Script .bat for generic Java compilation

File structure

The Java project must follow a simple organisation:

The script presented on this page
Folder containing Java sources (.java) of the project to compile
Optional folder for Java libraries (.jar) used by the project
Output folder where will automatically be placed the resulting compiled classes (.class)


  1. The script searches in the classic installation paths (such as C:\Program Files\Java\) for the best (most recent) version of Java available (since Java version 5, that is 1.5.0), with a priority for JDK over JRE.
  2. The script builds a list of source files .java that are in .\src\.
  3. The script builds a list of libraries .jar that are in .\lib\.
  4. The script compiles the above with javac.exe into .\bin\


This batch requires at least Windows 2000 (XP, Vista, Win7…).


@echo off
echo 	======== Java batch compile by Alexandre Alapetite ========
echo 	== 2007-09-30 / 2011-09-09 ==

rem Defines preferred Java path
rem set JAVA_HOME=C:\Program Files\Java\jdk1.7.0
if defined JAVA_HOME (
if exist "%JAVA_HOME%\bin\javac.exe" goto okJavac
rem If the above path does not exist, try to find the latest Java
set JAVA_ROOT=%ProgramFiles%\Java\
if not exist "%JAVA_ROOT%\" set JAVA_ROOT=%ProgramFiles(x86)%\Java\
if not exist "%JAVA_ROOT%\" set JAVA_ROOT=%SystemDrive%\Java\
if not exist "%JAVA_ROOT%\" set JAVA_ROOT=%HOMEDRIVE%\Java\
if not exist "%JAVA_ROOT%\" set JAVA_ROOT=%SystemDrive%\
echo Search Java JDK...
for /F "usebackq delims==" %%f in (`dir "%JAVA_ROOT%jdk*" /B /O:-N`) do if exist "%JAVA_ROOT%%%f\bin\javac.exe" (
goto foundJava
echo Search Java JRE...
for /F "usebackq delims==" %%f in (`dir "%JAVA_ROOT%jre*" /B /O:-N`) do if exist "%JAVA_ROOT%%%f\bin\javac.exe" (
goto foundJava
rem Remove quotes around JAVA_HOME, which are
rem necessary until there to avoid a bug with directories
rem which name contains a closing parenthesis, such as "C:\Program Files (x86)"
for /F "useback tokens=*" %%s in ('%JAVA_HOME%') do set JAVA_HOME=%%~s
if exist "%JAVA_HOME%\bin\javac.exe" goto okJavac
echo Cannot find Java compiler. Please install Java JDK or edit %%JAVA_HOME%% path.
goto end
echo Java path "%JAVA_HOME%"
"%JAVA_HOME%\bin\javac.exe" -version

rem Compile .\src\\*.java into .\bin\\*.class using libraries in .\lib\\*.jar
subst m: %0\..
rem "%0\.." is the real path of the batch file
pushd m:
cd \
echo Search java sources in .\src\...
dir src\*.java /B/S > javasrc.tmp~
echo Cannot find Java source files in .\src\
goto abort
echo Search jar libraries in .\lib\...
if exist lib for /F "usebackq delims==" %%l in (`dir lib\*.jar /B/S`) do echo -classpath %%l >> javasrc.tmp~
echo Compile in .\bin\...
if exist bin rmdir /S /Q bin
mkdir bin
echo on
@"%JAVA_HOME%\bin\javac.exe" -d bin @javasrc.tmp~
@echo off
echo Done.
del javasrc.tmp~
subst m: /d

echo on


1.3 2011-09-09
Added support for multiple libraries, and simplification
1.2 2011-01-07
Compatibility with folders, which name contains a closing parenthesis (bug of the batch parser), such as “C:\Program Files (x86)\
1.1 2008-05-15
Detection of the real path of the batch file with %0\..
1.0 2007-11-25
First distribution


This content is protected by a licence Creative Commons Attribution-ShareAlike 2.0 France “BY-SA (FR)” [Creative Commons License]


object: View comments