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.
The Java project must follow a simple organisation:
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
%0\..
This content is protected by a licence
Creative Commons Attribution-ShareAlike 2.0 France “BY-SA (FR)”