This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Help for first Java program

 
Ranch Hand
Posts: 52
1
MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic