wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help for first Java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help for first Java program" Watch "Help for first Java program" New topic
Author

Help for first Java program

Stephen Black
Ranch Hand

Joined: Aug 13, 2009
Posts: 50

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


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2250
    
  47
The Java runtime environment did not seem to want to run anything unless it was in the same folder as the java program

If by this you mean put your class files in the 'bin' subdirectory of the Java installation directory then don't ever do this. This is a fudge to get around the fact that you haven't set up your system properly and/or don't understand the basics about classpaths.

Please read the forum's Java FAQ which explains how to set up Java and run your first program.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Help for first Java program