It is a really simple thing but I cannot get my head around it. I have looked at plenty of StackOverFlow post and as well as on internet.
My goal is to create a .bat whi
Open Notepad
Type in the following:
javac *
java Main
SaveAs Main.bat or whatever name you wish to use for the batch file
Make sure that Main.java is in the same folder along with your batch file
Double Click on the batch file to run the Main.java file
open notepad
write
@echo off
javac Main.java
java Main
3.saveAs blahblah.bat
make sure that Main.java resides with your batch file and java path is set in env. variable
4 . double click on batch file, no need to open cmd explicitly tt will open itself on .bat execution
I've also faced a similar situation where I needed a script which can take care of javac and then java(ing) my java program. So, I came up with this BATCH script.
:: @author Rudhin Menon
:: Created on 09/06/2015
::
:: Auto-Concrete is a build tool, which monitor the file under
:: scrutiny for any changes, and compiles or runs the same once
:: it got changed.
::
:: ========================================
:: md5sum and gawk programs are prerequisites for this script.
:: Please download them before running auto-concrete.
:: ========================================
::
:: Happy coding ...
@echo off
:: if filename is missing
if [%1] EQU [] goto usage_message
:: Set cmd window name
title Auto-Concrete v0.2
cd versions
if %errorlevel% NEQ 0 (
echo creating versions directory
mkdir versions
cd versions
)
cd ..
javac "%1"
:loop
:: Get OLD HASH of file
md5sum "%1" | gawk '{print $1}' > old
set /p oldHash=<old
copy "%1" "versions\%oldHash%.java"
:inner_loop
:: Get NEW HASH of the same file
md5sum "%1" | gawk '{print $1}' > new
set /p newHash=<new
:: While OLD HASH and NEW HASH are the same
:: keep comparing OLD HASH and NEW HASH
if "%newHash%" EQU "%oldHash%" (
:: Take rest before proceeding
ping -w 200 0.0.0.0 >nul
goto inner_loop
)
:: Once they differ, compile the source file
:: and repeat everything again
echo.
echo ========= %1 changed on %DATE% at %TIME% ===========
echo.
javac "%1"
goto loop
:usage_message
echo Usage : auto-concrete FILENAME.java
Above batch script will check the file for any changes and compile if any changes are done, you can tweak it for compiling whenever you want. Happy coding :)
@author MADMILK
@echo off
:getback1
set /p jump1=Do you want to compile? Write yes/no :
if %jump1% = yes
goto loop
if %jump1% = no
goto getback2
:: Important part here
:getback2
set /p jump2=Do you want to run a java program? Write yes/no :
if %jump2% = yes
goto loop2
if %jump2% = no
goto getback1
:: Make a folder anywhere what you want to use for scripts, for example i make one.
cd desktop
:: This is the folder where is gonna be your script(s)
mkdir My Scripts
cd My Scripts
title Java runner/compiler
echo REMEMBER! LOWER CASES AND UPPER CASES ARE IMPORTANT!
:loop
echo This is the java compiler program.
set /p jcVal=What java program do you want to compile? :
javac %jcVal%
:loop2
echo This is the java code runner program.
set /p javaVal=What java program do you want to run? :
java %javaVal%
pause >nul
echo Press NOTHING to leave
you want these four lines of code in your Run.bat:
@echo off //this makes it so you have an empty cmd window on startup
javac Main.java //this compiles the .java into a .class
java Main // this runs the .class file
pause //this prevents the window from instantly closing after program end
The only other thing I would add is to make it a tad more flexible. Most times I'll have a trivial java file I want to run like - Main.java, Simple.java, Example.java, or Playground.java (you get the idea).
I use the following to strike off a javac and corresponding java.
@echo off
javac %~n1.java
java %~n1
The %~n1 gets the filename (sans extension) of the first argument passed to the batch file. So this way I can run it using tab completion and not have to worry about it working with either the .class or .java extension.
So both of the following will have the same result:
run.bat Main.class
and
run.bat Main.java
Doesn't 100% answer the original posters question, but I think it is a good next step/evolution for simple javac/java programs.