by Alexandre Alapetite on 2007-09-30; updated 2008-05-15

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 on 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, ...).

JavaCompile.bat

@echo off
echo 	== Java batch compile by Alexandre Alapetite ==
echo 	== 2008-05-15  http://alexandre.alapetite.fr ==

rem Defines preferred Java path
rem set JAVA_HOME=C:\Program Files\Java\jdk1.6.0
if defined JAVA_HOME (
if exist "%JAVA_HOME%\bin\java.exe" goto foundJava
)
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=%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 not exist "%JAVA_HOME%\bin\java.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 not exist "%JAVA_HOME%\bin\java.exe" (
set JAVA_HOME=%JAVA_ROOT%%%f
goto foundJava
)
:foundJava
if exist "%JAVA_HOME%\bin\java.exe" goto okJava
echo Cannot find Java. Please instal Java or edit %%JAVA_HOME%% path.
goto end
:okJava
echo Java path "%JAVA_HOME%"
"%JAVA_HOME%\bin\java.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 dir lib\*.jar /B/S > javalib.tmp~
echo Compile in .\bin\...
if exist bin rmdir /S /Q bin
mkdir bin
if exist lib (
echo on
@"%JAVA_HOME%\bin\javac.exe" -d bin -classpath @javalib.tmp~ @javasrc.tmp~
@echo off
) else (
echo on
@"%JAVA_HOME%\bin\javac.exe" -d bin @javasrc.tmp~
@echo off
)
echo Done.
:abort
del javasrc.tmp~
if exist lib del javalib.tmp~
popd
subst m: /d

:end
set JAVA_ROOT=
echo on

History

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

http://alexandre.alapetite.net

Back