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.

English

Script .bat for generic Java compilation

File structure

The Java project must follow a simple organisation:

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

Description

  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\

Script

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

JavaCompile.bat

@echo off
echo 	======== Java batch compile by Alexandre Alapetite ========
echo 	== 2007-09-30 / 2011-09-09 https://alexandre.alapetite.fr ==

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" (
set JAVA_HOME="%JAVA_ROOT%%%f"
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" (
set JAVA_HOME="%JAVA_ROOT%%%f"
goto foundJava
)
: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
:okJavac
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~
if ERRORLEVEL 1 (
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.
:abort
del javasrc.tmp~
popd
subst m: /d

:end
set JAVA_ROOT=
echo on

History

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

Licence

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


Comments

object: View comments

https://alexandre.alapetite.fr

Back