Monday Sep 15, 2008

A nasty Problem - Java Code

Few days back, I got a problem in which actually I have to get the fields, constructor and method name from a java class. One can think of using reflection API and the problem is easy to solve, but that is not the case. Reflection API can only be used on class files not Java file. Than a quick idea came into mind to compile the file internally, use reflection and then delete the class file, if user don't want to see it. This is never be a tough job if I use JDK6 JavaCompiler code which is :

import java.io.IOException;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class JDK6FirstCompile {
  public static void main(String args[]) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int results = compiler.run(null, null, null, "HelloWorld.java");
    System.out.println("Success: " + (results == 0));
  }
}


class HelloWorld {
  public static void main(String args[]) {
    System.out.println("Hello, World");
  }
}


But but, I was constraint to use JDK5 or less. So, this JDK6 API is not worth for me :-(. Finally, came up with a small code, which probably can do this in all the case. Hoping it will not lose any generic behavior and run in all cases.

Here is the code:

import java.io.\*;

public class CompileCheck {

public void compileClasses2() {
        try {
            String command = System.getProperty("java.home")+File.separator + ".." + File.separator+"bin"+File.separator+"javac " + "HelloWorld.java";
            System.out.println(command);
            Runtime.getRuntime().exec(command);
    } catch (Exception e) { }
    }


    public static void main(String[] args) {
 
        CompileCheck c = new CompileCheck();
        c.compileClasses2();
    }
}


In place of HelloWorld, I can surely use any path where the java file resides. And off course, I can use \*.java as well :-). Please let me know your opinion or any other method easier than this from which I can get class methods, attributes and all from a java file.

Tuesday Jun 03, 2008

Compilation on Fly !

Weeks back, I had a discussion with one of my colleagues on classLoader. Actually his classLoader is not able to perform certain job but then suddenly he asked me about any option in Java from which I can generate class file at runtime. Yes, why not. This is how JSP Compiler,Jasper works. There are some of the older way to do this in previous Java. But from J2SE 6 and onwards, one can do it neatly. 

J2SE 6 has added a package called javax.tools which has some awesome work to do. And the above task is one of it. We are going to use a class call ToolProvider which provides method to locate tools like Compiler :-).

Here I have written a simple code which go ahead and compile all the java files in a given directory at runtime and show the result.

import java.io.\*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class RuntimeCompilation {

    public static void main(String args[]) throws IOException {
        String dirName = "E:\\\\Program Files\\\\Java\\\\jdk1.6.0\\\\bin\\\\compiler";
        File f = new File(dirName);
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (f.isDirectory() == false) {
            System.out.println("Directory is not correct");
        } else {
            String lists[] = f.list();
            for (int i = 0; i < lists.length; i++) {
                if (lists[i].endsWith("java")) {
                    int results = compiler.run(null, null, null, lists[i]);
                    System.out.println("Compiling ..." + lists[i] + "      " + (results == 0));
                }
            }
        }
    }
}
 

I have 4-5 Java files at this location: E:\\Program Files\\Java\\jdk1.6.0\\bin\\compiler. So, simply checked the directory and then list the file and pass it into the compilation process, thats it.Code is not written cleanly but do the basic job :). javax.tools package is quite amazing, have a look !

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