I found it hard to compile and run my first java program
The Java runtime environment did not seem to want to run anything unless it was in the same folder as the java program
and various other things made it confusing
anyhow I ended up with 2 working batch files that compile and run java programs seamlessly
And I thought that I would share them with anybody else who is having trouble with their first java program as many do
Since I can't attach it here is the compiling batchfile
@echo off
set %FileName% = %1
REM Set Script Directory so we know where to come back to
set PreDir=%CD%
echo It is recommended that you read through the well documented script
echo and you will need to create some environmental variables
REM Change directory to the Java Compiler directory
CD/
cd %JavaCompiler%
cls
REM Start the Java Compiler and pass it the java file
echo ----------------------------------------------------------
echo Compiling Java
echo ----------------------------------------------------------
echo.
Javac -g %PreDir%\%1.java
REM echo new lines
echo.
echo.
echo ---------------------------------------
echo Done
echo ----------------------------------------
REM Return to Script Directory
cd %PreDir%
echo If the script fails here then you need to Change the script at this point as instructed in script text
REM you may need to do 2 things to make this script work
Rem 1. alter the line of script to point to the right directory on your computer
REM 2. grant yourself higher privelages in the below directory so that the file can be moved there
REM Move the class file to the directory for execution
move %1.class "C:\Program Files (x86)\Java\jre7\bin\%1.class"
REM run the class file using the JRunMate.bat file
CALL JRunMate.bat %1
REM Return to Script Directory
cd %PreDir%
And here is the batch file that runs the java program
@echo off
echo ---------------------------------
REM Set Script Directory so we know where to come back to
set %PreDir%=%CD%
REM Change directory to the Java Virtual machine directory (Which also holds the compiler)
CD/
cd %JavaCompiler%
REM Start the Java Compiler and pass it the java file
echo ----------------------------------------------------------
echo Running Java
echo ----------------------------------------------------------
echo.
REM Run the class
Java %1
REM echo new lines
echo.
echo.
echo --------------------------------------------
echo Java program Completed
echo --------------------------------------------
echo.
REM Return to Script Directory
cd %JFiles%
Rem Delete the class files
echo DON'T worry about files not being found error message
DEL /Q "C:\Program Files (x86)\Java\jre7\bin\"%1.class
DEL /Q %JFiles%%1.class
The Java runtime environment did not seem to want to run anything unless it was in the same folder as the java program
and various other things made it confusing
anyhow I ended up with 2 working batch files that compile and run java programs seamlessly
And I thought that I would share them with anybody else who is having trouble with their first java program as many do
Since I can't attach it here is the compiling batchfile
@echo off
set %FileName% = %1
REM Set Script Directory so we know where to come back to
set PreDir=%CD%
echo It is recommended that you read through the well documented script
echo and you will need to create some environmental variables
REM Change directory to the Java Compiler directory
CD/
cd %JavaCompiler%
cls
REM Start the Java Compiler and pass it the java file
echo ----------------------------------------------------------
echo Compiling Java
echo ----------------------------------------------------------
echo.
Javac -g %PreDir%\%1.java
REM echo new lines
echo.
echo.
echo ---------------------------------------
echo Done
echo ----------------------------------------
REM Return to Script Directory
cd %PreDir%
echo If the script fails here then you need to Change the script at this point as instructed in script text
REM you may need to do 2 things to make this script work
Rem 1. alter the line of script to point to the right directory on your computer
REM 2. grant yourself higher privelages in the below directory so that the file can be moved there
REM Move the class file to the directory for execution
move %1.class "C:\Program Files (x86)\Java\jre7\bin\%1.class"
REM run the class file using the JRunMate.bat file
CALL JRunMate.bat %1
REM Return to Script Directory
cd %PreDir%
And here is the batch file that runs the java program
@echo off
echo ---------------------------------
REM Set Script Directory so we know where to come back to
set %PreDir%=%CD%
REM Change directory to the Java Virtual machine directory (Which also holds the compiler)
CD/
cd %JavaCompiler%
REM Start the Java Compiler and pass it the java file
echo ----------------------------------------------------------
echo Running Java
echo ----------------------------------------------------------
echo.
REM Run the class
Java %1
REM echo new lines
echo.
echo.
echo --------------------------------------------
echo Java program Completed
echo --------------------------------------------
echo.
REM Return to Script Directory
cd %JFiles%
Rem Delete the class files
echo DON'T worry about files not being found error message
DEL /Q "C:\Program Files (x86)\Java\jre7\bin\"%1.class
DEL /Q %JFiles%%1.class