[java] How to run java application by .bat file

I need to run my Java Application through .bat file. Can anybody help please.

This question is related to java

The answer is


Sure, call the java executable.

Mine is C:\Program Files\Java\jre6\bin\java.exe, so to run it I would do

C:\Program Files\Java\jre6\bin\java.exe -jar myjarfile.jar


If You have jar file then create bat file with:

java -jar NameOfJar.jar

@echo off
echo You Are going to creata Java Class
set /p Name=Enter your Class Name?:
echo Your class Name is %Name% & pause
echo To creat a Notepad
pause
notepad %Name%.java
set path=%PATH%;C:\Program Files\Java\jdk1.6.0_14\bin
pause
javac
echo Your java Path succsussfully set.
javac %Name%.java
pause
echo Successfully Compiled
java %Name%
pause

1)open a notpad 2)copy and past this code and save this file as ex: test.bat 3)Double Click tha batch file. 4)put your java codes into the notepad and save it as N.B.:- save this java file same folder that your batch file exists.


Call the class which has main() method.

java MyClass

Here MyClass will have public static void main() method.


javac Application.java

java Application

pause

The javac command will compile the java program and the java command will run the program and pause will pause the result until you cross it.


It's the same way you run it from command line. Just put that "command line" into a ".bat" file.

So, if you use java -cp .;foo.jar Bar, put that into a .bat file as

@echo off

java -cp .;foo.jar Bar

  • javac (.exe on Windows) binary path must be added into global PATH env. variable.

    javac MyProgram.java

  • or with java (.exe on Windows)

    java MyProgram.jar