Forums Register Login

Help for first Java program

+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
 

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.
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1284 times.
Similar Threads
question about weblogic 6.1
How to make exe in JBuilder 3 Professional
JBoss run.bat not launching not showing any errors just going to next line in the command prompt
Syntax of the command is incorrect msg when starting startup.bat from command line
Error in starting Jboss through Command Prompt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:51:55.