Interaction with Batch File from Java Code

Most of the time when we start server, we write lot of code to optimize the condition. Most of those things are tough to write in Java but easy for batch file or sh file to work on. Like, go into a folder, set JAVA_HOME, setting some heap size and then fire the server. Things are not always start forward, sometime we need to pass some message or some path or some value from Java code to batch file.

Here I tried to write one. Say my JAVA_HOME is dynamic and on some condition I decide what's going to be JAVA_HOME and further on that what java and javac going to run(means setting path). 

import java.io.\*;
public class batchCheck {
    public static void main(String[] args) {
        //All JavaHomes
        String javaHome[] = {"E:\\\\Program Files\\\\Java\\\\j2sdk1.4.2_05", "E:\\\\Program Files\\\\Java\\\\jdk1.6.0"};
        String path;
        String line = "";
        String pathFile = "E:\\\\Program Files\\\\Java\\\\jdk1.6.0\\\\bin\\\\JavaOutput";
        String whichJDK = "";
        String decisionMaker = "142";
        if (decisionMaker.equals("142")) {
            whichJDK = javaHome[0];
        } else {
            whichJDK = javaHome[1];
        }
        try {
            path = whichJDK + "\\\\bin";
            String cmds[] = {"check.bat", pathFile, whichJDK, path};
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(cmds);
            proc.getOutputStream().close();
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            while ((line = bufferedreader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now, you can see my check.bat file:

cd %1
DEL HelloWorld.class
set PATH=%3
set JAVA_HOME=%2
echo %JAVA_HOME%
javac HelloWorld.java
java HelloWorld
exit
 

So, see how we can access the values from java file into batch file from %1, %2, %3 ... ($1,$2... in sh). Simply I moved on to the path where HelloWorld.java resides. I have deleted the old class file and the set the path, set the JAVA_HOME, compiled with javac of new JAVA_HOME and ran the code + exit :D.

Lot many things have been done from a very simple code.

Comments:

There are lots of tasks for which a batch file is the right answer.

You can get a full featured Batch file debugger which allows you to step through your code, add breakpoints, etc. Plus it analyzes your code to point errors and warnings. The product name is 'Running Steps'. Check it out at http://www.steppingsoftware.com.

Posted by giskier on May 31, 2008 at 06:59 PM IST #

please tell me how to fetch data to the command prompt that is generated when a batch file is run.
i.e when i run few commands using a batch file for creating certificates it asks for some details like name of country,name of organization n so on I have a file that has all the data in a serial order.now when this prompts for this data i need to fetch from this file one after the other
hence please help me in getting this executed....
thanks a lot !!

Posted by guest on April 20, 2010 at 11:54 AM IST #

Post a Comment:
Comments are closed for this entry.
About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today