How to create a batch file to run a Java application

Discussion in 'Java' started by Cyril Dex, Oct 13, 2003.

  1. Cyril Dex

    Cyril Dex Guest

    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
    Cyril Dex, Oct 13, 2003
    #1
    1. Advertising

  2. Cyril Dex schrieb:

    > 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!
    Thomas Kellerer, Oct 13, 2003
    #2
    1. Advertising

  3. Cyril Dex

    Bryce (Work) Guest

    On 13 Oct 2003 08:21:38 -0700, (Cyril Dex)
    wrote:

    >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>
    Bryce (Work), Oct 13, 2003
    #3
  4. Cyril Dex

    Paul Lutus Guest

    Cyril Dex wrote:

    > 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.

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Oct 15, 2003
    #4
  5. Cyril Dex

    jeevana

    Joined:
    May 15, 2009
    Messages:
    1
    :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.
    jeevana, May 15, 2009
    #5
  6. Cyril Dex

    jobinvs

    Joined:
    Apr 21, 2010
    Messages:
    2
    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: Apr 21, 2010
    jobinvs, Apr 21, 2010
    #6
  7. Cyril Dex

    jobinvs

    Joined:
    Apr 21, 2010
    Messages:
    2
    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.
    jobinvs, Apr 21, 2010
    #7
  8. Cyril Dex

    sankalp.sawant59

    Joined:
    Mar 10, 2013
    Messages:
    1
    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..
    sankalp.sawant59, Mar 10, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike John

    How to run a batch file.

    Mike John, Jul 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    411
    Saravana
    Jul 23, 2003
  2. =?Utf-8?B?WjBnUw==?=

    Use process class to run batch file in web server

    =?Utf-8?B?WjBnUw==?=, Feb 17, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,705
    =?Utf-8?B?WjBnUw==?=
    Feb 18, 2004
  3. viki

    run batch file in client side

    viki, Nov 14, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    25,159
    dotman112
    Aug 20, 2010
  4. Mozart
    Replies:
    7
    Views:
    3,816
  5. viki

    TO run an Application from Batch file

    viki, Oct 24, 2004, in forum: ASP .Net Security
    Replies:
    4
    Views:
    298
    Paul Clement
    Oct 27, 2004
Loading...

Share This Page