JDK-6450729 : Getting NullPointerExcpetion in CommandLine.java method (lin#45) while compiling
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-07-20
  • Updated: 2016-04-19
  • Resolved: 2006-07-24
Related Reports
Relates :  
Description
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.

Comments
EVALUATION Lines 44-45 are: 44 String arg = args[i]; 45 if (arg.length() > 1 && arg.charAt(0) == '@') { args is the array coming from the submitted example and so it must have a null element which is not valid input.
24-07-2006