How to create a batch file to run a Java application


C

Cyril Dex

Hey guys

Does anyone know of where I can get information on how to get a batch
file to change directory into where class files are located and then
rum those java files - by double clicking on it.

e.g. set $PATH="c:/j2sdk1.4.2/bin" //where the java compiler is
located
cd c:/MyProgram
javac MyProgram.java
java MyProgram
 
Ad

Advertisements

T

Thomas Kellerer

Cyril said:
Hey guys

Does anyone know of where I can get information on how to get a batch
file to change directory into where class files are located and then
rum those java files - by double clicking on it.

e.g. set $PATH="c:/j2sdk1.4.2/bin" //where the java compiler is
located
cd c:/MyProgram
javac MyProgram.java
java MyProgram

This is a Windows question not a Java question, but nevertheless:

cd /d c:\MyProgram

I can recommend cd /? as well!
 
B

Bryce (Work)

Hey guys

Does anyone know of where I can get information on how to get a batch
file to change directory into where class files are located and then
rum those java files - by double clicking on it.

e.g. set $PATH="c:/j2sdk1.4.2/bin" //where the java compiler is
located
cd c:/MyProgram
javac MyProgram.java
java MyProgram


1. YOu don't need to compile it each time you run it. Only when you've
changed something.

2.
set $PATH=$PATH;"c:java bin dir"


Although I generally don't do that. I'll create an environment
variable "JAVA_HOME", and set path to $JAVA_HOME/bin.

then my bat file is:

java -cp <my class path> <path to my program class>
 
P

Paul Lutus

Cyril said:
Hey guys

Does anyone know of where I can get information on how to get a batch
file to change directory into where class files are located and then
rum those java files - by double clicking on it.

e.g. set $PATH="c:/j2sdk1.4.2/bin" //where the java compiler is
located
cd c:/MyProgram
javac MyProgram.java
java MyProgram

You don't need to compile every time. And do it this way:

java -cp /MyProgram MyProgram

No need to change directories.
 
Joined
May 15, 2009
Messages
1
Reaction score
0
:captain:

@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 <name.bat>
ex: test.bat
3)Double Click tha batch file.
3)put your java codes into the notepad and save it as <your_java_class_name.java>
N.B.:- save this java file same folder that your batch file exists.
 
Joined
Apr 21, 2010
Messages
2
Reaction score
0
Java programs run using batch file. I have tried this and is working fine.

Hope this post will be useful for someone in search of this:
Batch Files.
1st Batch File:"RunJava.bat"
@echo off
cd /d %~dp0
call "D:\JavaPrograms\source\source.bat"
Pause
echo "Program Executed"

2nd Batch File:"sourece.bat"
@echo off
cd /d %~dp0
ECHO.
echo **************************************************************
dir D:\JavaPrograms\source\*.java
ECHO.
echo **************************************************************
ECHO.
set /p Name=Enter one Java file name from above list for execution:
javac -d ..\Classes %Name%.java
call D:\JavaPrograms\classes\Classes.bat %1 %Name%
echo "Successfully Executed"


3rd Batch File:"Classes.bat"

@echo off
cd /d %~dp0
echo Name=%Name%
echo Output of the Program
echo ---------------------
java %Name%
call "C:\Documents and Settings\Administrator\Desktop\RunJava.bat"

Working Help
In My system:
1. The RunJava.bat is place in the desktop. I write java programs in notepad or Editplus and save programs in "D:\JavaPrograms\source"
2. The "source.bat" is placed in "D:\JavaPrograms\source".
3.The compiled classes file are stored in "D:\JavaPrograms\Classes".
4.This is done with the help of "javac -d ..\Classes %Name%.java" <- compile and store the class file in ..\Classes folder
5.The "source.bat" calls "Classes.bat"
6.The "Classes.bat" in placed in"D:\JavaPrograms\Classes."
7.The "Classes.bat" runs the java program.

Batch File: RunJava.bat
1.Place this file any where in your computer.
2.This batch file is executed in the current directory through
cd /d %~dp0
call "D:\JavaPrograms\source\source.bat"
This is the place where the java source files are stored also this is the place for source.bat.




Batch File: source.bat
1.This batch file compiles the .java file.
2.dir D:\JavaPrograms\source\*.java: lists all the java programs in the source folder
3.set /p Name=Enter one Java file name from above list for execution:
The above statement accepts the program name like "HelloWorld" without .java extension

which needs to be compiled. Do not specify the name of the program as "HelloWorld.java".
4.javac -d ..\Classes %Name%.java: Compiles the "HelloWorld" java file and places the compiled class file under the Classes folder.
5.call D:\JavaPrograms\classes\Classes.bat %1 %Name% : Calls the Classes.bat file which is residing in Classes folder.







Batch File: Classes.bat
1.Classes.bat resides inside Classes folder.
2.This batch file runs the java program "java %Name%";
3.call "C:\Documents and Settings\Administrator\Desktop\RunJava.bat": Call the RunJava.bat file.
 
Last edited:
Ad

Advertisements

Joined
Apr 21, 2010
Messages
2
Reaction score
0
Cyril Dex said:
Hey guys

Does anyone know of where I can get information on how to get a batch
file to change directory into where class files are located and then
rum those java files - by double clicking on it.

e.g. set $PATH="c:/j2sdk1.4.2/bin" //where the java compiler is
located
cd c:/MyProgram
javac MyProgram.java
java MyProgram
Hope this post will be useful for someone in search of this:
Batch Files.
1st Batch File:"RunJava.bat"

@echo off
cd /d %~dp0
call "D:\JavaPrograms\source\source.bat"
Pause
echo "Program Executed"

2nd Batch File:"sourece.bat"

@echo off
cd /d %~dp0
ECHO.
echo ************************************************** ************
dir D:\JavaPrograms\source\*.java
ECHO.
echo ************************************************** ************
ECHO.
set /p Name=Enter one Java file name from above list for execution:
javac -d ..\Classes %Name%.java
call D:\JavaPrograms\classes\Classes.bat %1 %Name%
echo "Successfully Executed"


3rd Batch File:"Classes.bat"

@echo off
cd /d %~dp0
echo Name=%Name%
echo Output of the Program
echo ---------------------
java %Name%
call "C:\Documents and Settings\Administrator\Desktop\RunJava.bat"

Working Help
In My system:
1. The RunJava.bat is place in the desktop. I write java programs in notepad or Editplus and save programs in "D:\JavaPrograms\source"
2. The "source.bat" is placed in "D:\JavaPrograms\source".
3.The compiled classes file are stored in "D:\JavaPrograms\Classes".
4.This is done with the help of "javac -d ..\Classes %Name%.java" <- compile and store the class file in ..\Classes folder
5.The "source.bat" calls "Classes.bat"
6.The "Classes.bat" in placed in"D:\JavaPrograms\Classes."
7.The "Classes.bat" runs the java program.

Batch File: RunJava.bat
1.Place this file any where in your computer.
2.This batch file is executed in the current directory through
cd /d %~dp0
call "D:\JavaPrograms\source\source.bat"
This is the place where the java source files are stored also this is the place for source.bat.




Batch File: source.bat
1.This batch file compiles the .java file.
2.dir D:\JavaPrograms\source\*.java: lists all the java programs in the source folder
3.set /p Name=Enter one Java file name from above list for execution:
The above statement accepts the program name like "HelloWorld" without .java extension

which needs to be compiled. Do not specify the name of the program as "HelloWorld.java".
4.javac -d ..\Classes %Name%.java: Compiles the "HelloWorld" java file and places the compiled class file under the Classes folder.
5.call D:\JavaPrograms\classes\Classes.bat %1 %Name% : Calls the Classes.bat file which is residing in Classes folder.







Batch File: Classes.bat
1.Classes.bat resides inside Classes folder.
2.This batch file runs the java program "java %Name%";
3.call "C:\Documents and Settings\Administrator\Desktop\RunJava.bat": Call the RunJava.bat file.
 
Ad

Advertisements

Joined
Mar 10, 2013
Messages
1
Reaction score
0
sql queries compilation using bat file...

thanx...i want to do similar stuff for sql quries...wat i have to do..post code for that...thanx in advance..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top