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.

Sunday Jul 08, 2007

jikes vs javac

jikes is something which converts .java file into .class file. Yes, it
is javac but IBM product not a Sun product. And let me begin with jikes
page on sourceforge.net :

Some comparisons:

No. 1 : jikes is a open source software.
And so javac is ! Didn't get the advantage.

No. 2: Strictly Java compatible - It is not providing any superset or subset variation support.

Good, but this little of strictness most of time increase the code line.

No. 3: High Performance: Compilation is faster than javac.

Who
really bother about compilation, anyway you have to come again and run
on java command. I guess world is ready to devote 2 more minute in
compilation.

Anyway, I have compiled one of the .java file(one which i have used in the last post) on the most stable version of jikes:

jikes TestForJDB.java

Issued 5 system warnings:

\*\*\* Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


\*\*\* Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


\*\*\* Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


\*\*\* Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


\*\*\* Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

Didn't get what this warning message is and really not why you show it to me 5 times.

Then I have tried to look into -version command.

javac -version

- javac 1.6.0

jikes -version

- Jikes Compiler - Version 1.22 - 3 October 2004
Copyright (C) IBM Corporation 1997-2003, 2004.
- Licensed Materials - Program Property of IBM - All Rights Reserved.
Originally written by Philippe Charles and David Shields of IBM Research,
Jikes is now maintained and refined by the Jikes Project at:
Please consult this URL for more information and for reporting problems.

Very informative :)). Originally written by ... what does that mean ?



No 4. jikes provides some cool feature like incremental build.

I didn't get anything out of it.

Look at this feature.

jikes ++ TestForJDB.java

// After the 5 warning message.

Incremental: Enter to continue or q + Enter to quit:

nothing changed...

Incremental: Enter to continue or q + Enter to quit: (this if you change the code)

ok...

Incremental: Enter to continue or q + Enter to quit:

nothing changed...


No 5. clear error message.

I have removed one semi-colon from the code at line no. 25. Look at the error message printed by two compilers:

javac TestForJDB.java

TestForJDB.java:25: ';' expected
stringJDB = value
\^
1 error

jikes TestForJDB.java

Found 1 syntax error in "E:/Program Files/Java/jdk1.6.0/bin/TestForJDB.java":

25. stringJDB = value
\^---------------\^
\*\*\* Syntax Error: ";" inserted to complete BlockStatementsopt

Are you able to find out something extra ? By the way what was the meaning of BlockStatementsopt ?


I guess I made too many of negative statement :), Ok some good features are here:

- it is providing some additional options like

> jikes +Punused-package-imports TestForJDB.java - which at compilation time, will show you those import statement which are not in use.

> jikes +Pnaming-convention testForJDB.java - it will throw warning if you are not following the naming convention ( its really an idea of Sun, don't mind :) )


Again,
I didn't like the verbose information(javac -verbose filename.java) .
Where is the time information man... parsing time, total time to make
class file... these are some of the cool use of verbose option. Showing
only which all files you are reading is a useless information.

Anyway, nice to see one step ahead in the world of open source.

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