24

I need to run my Java Application through a .bat file.

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
vivek
  • 4,599
  • 3
  • 25
  • 37

8 Answers8

34

Simply create a .bat file with the following lines in it:

@ECHO OFF
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;path/to/needed/jars/my.jar

%JAVA_HOME%\bin\java -Xms128m -Xmx384m -Xnoclassgc ro.my.class.MyClass
panagdu
  • 2,133
  • 1
  • 21
  • 36
14

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

A T
  • 13,008
  • 21
  • 97
  • 158
  • 3
    That should be `java -jar myjarfile.jar` (your example is missing the `-jar`) –  Feb 18 '13 at 17:54
8

If You have jar file then create bat file with:

java -jar NameOfJar.jar
Pawel
  • 798
  • 8
  • 28
7

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
adarshr
  • 61,315
  • 23
  • 138
  • 167
3
@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.

Karan Shah
  • 744
  • 5
  • 15
2

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.

edhedges
  • 2,722
  • 2
  • 28
  • 61
M.Zaheer
  • 21
  • 1
0

Call the class which has main() method.

java MyClass

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

Gaurav
  • 1,549
  • 2
  • 15
  • 31
0
  • 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
    
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Salaros
  • 1,444
  • 1
  • 14
  • 34