Friday May 16, 2008

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.

Thursday May 15, 2008

Weekend with registry and JDK :)

Last weekend, I was trying to find out all the JDK installed in my machine and its path(JAVA_HOME) as well. Just want to see that shall I control all JDK from one place. Most of time it happens with me, I need to check my code on several JDK and finally everything messed up. Setting JAVA_HOME, setting PATH and many more.

So, I see if I get to know the path of java and javac of all JDK versions, I can compile it and even run it from a single location. Solaris and Linux sounds similar in this case but the problem was to find it on Windows. Off course, checking some registry entries of installation and finally I wrote a small code which can tell you all the JDK version and PATH of it on your local machine. From here, you can download the jar file from  HERE (for windows)and if you want the code, please let me know. Now, need to move further and work on my problem of various JDK version.

Surprisingly joke happened with my friend, when I was given him this jar file. He told me I have only JDK 1.5.0_14 on my machine and I am not using any other because this is the most stable version of JDK(I don't know on which basis, he was making that conclusion). He ran my jar file and came up with 2 versions of JDK 1.3, 1 version of JDK1.4.2, 3 version of JDK 1.5 and 1 version of JDK 1.6. Oh God, I am wasting so much of space :)).

 

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