FULL PRODUCT VERSION :
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
I have a method as below -
private void compileClasses(String directoryName)
{
File base = new File(directoryName);
File[] content = base.listFiles();
String[] sourceFiles = new String[content.length + 4];
//Sepecify runtime arguments like classpath and output path for compiling classes
sourceFiles[0] = "-classpath";
sourceFiles[1] = CLASSPATH; //some classpath
sourceFiles[2] = "-d";
sourceFiles[3] = OUTPUT_DIR; //some output path
//Code here to populate *.java files from given directoryName.
//For ex :- sourceFiles[] = {"-classpah", "c:\jars", "-d", "c:\projects", "TimeCalculator.java", "CurrancyCalculator.java" ..etc }
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
int errorCode = javac.compile(sourceFiles, printWriterObject);
if(errorCode != 0) //ERROR
//Print ERROR
}
When I run this method, I get the below error ->>
An exception has occurred in the compiler (1.5.0_04). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.NullPointerException
at com.sun.tools.javac.main.CommandLine.parse(CommandLine.java:45)
at com.sun.tools.javac.main.Main.compile(Main.java:570)
at com.sun.tools.javac.main.Main.compile(Main.java:544)
at com.sun.tools.javac.Main.compile(Main.java:85)
at com.iconixx.generator.ClassInstaller.compileClasses(ClassInstaller.java:140)
The errorCode returned by the "compile" method is 4 and I believe it means ABNORMAL.
Strange thing is, when I run the above compile method in little different way to compile one class at a time instead of all the classes (or more than one) from the given directory using below code, it works fine.
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
int status = javac.compile(new String[] {
"-classpath", CLASSPATH,
"-d", OUTPUT_DIR,
"-Xlint:unchecked",
fileObject.getAbsolutePath()} ,
printWriterObject);
Please look into it.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code given above. When more than one classes are given in String[] for compilation, NullPointerException is being thrown. It works fine to compile one class at a time.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result is that all the java source files in a given directory should get compiled in one call instead of one call per source file.
ACTUAL -
NullPointerException stops the entire process and nothing works.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Given in description. Giving again.
An exception has occurred in the compiler (1.5.0_04). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.NullPointerException
at com.sun.tools.javac.main.CommandLine.parse(CommandLine.java:45)
at com.sun.tools.javac.main.Main.compile(Main.java:570)
at com.sun.tools.javac.main.Main.compile(Main.java:544)
at com.sun.tools.javac.Main.compile(Main.java:85)
at com.iconixx.generator.ClassInstaller.compileClasses(ClassInstaller.java:140)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Pls see the description.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Compile one class at a time instead of more number of classes in a single call "compile(String[], PrintWriter)". But for the classes which have interdependancy, this workaround wont really help. i need a way which works similar to "javac *.java". In short a way which compiles everything in a folder in just one call and resolve dependancy internarlly.