A Windows Batch File to Compile Java Programs Without IDEs

Today in class, I taught my students how to compile their Java programs without using any IDE — only the command prompt in Windows.

We just created a batch file that automates the setting of the PATH variable, the compilation and the execution of the program itself.

@echo off
set PATH=%PATH%;C:\Program Files\Java\jdk1.7.0\bin
set /p CLASSNAME="Enter class name: "
javac %CLASSNAME%.java
java %CLASSNAME%
pause

We saved it using the file name: compiler.bat and just call it every time we want to compile our Java programs.

Take note that in setting the PATH variable, you will have to specify where the javac.exe program is located. In my case, it is under “C:\Program Files\Java\jdk1.7.0\bin”.

The assumption here is that the file that you want to compile is saved under the same directory where your compiler.bat is located.

You can even add your own messages in the batch file:

@echo off
set PATH=%PATH%;C:\Program Files\Java\jdk1.7.0\bin
set /p CLASSNAME="Enter class name: "
javac %CLASSNAME%.java

echo Compiling... please wait for a while!
echo.

echo This is the output:
echo.
java %CLASSNAME%

echo.
pause

The echo allows you to print strings in the command prompt. The echo. allows you to print newline characters.

One thought on “A Windows Batch File to Compile Java Programs Without IDEs”

Leave a Reply