JDK-6333296 : javac -source 1.4 -target 1.4 generates code that cannot be executed in 1.4
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2005-10-06
  • Updated: 2011-02-16
  • Resolved: 2005-10-07
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Client VM (build 1.5.0_03-b07, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux 2.6.3-19mdk #1 Thu Sep 23 22:04:58 MDT 2004 i686 unknown unknown GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Using the "javac" tool with the "-source 1.4 -target 1.4" options generates class files that cannot be executed within a version 1.4 runtime.  As one specific example, the String.contains() method--which is listed in the API as "Since: 1.5"--is allowed and compiled without warnings or errors, but the class file is unusable in 1.4.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the code below using javac v1.5:
$JAVA5/bin/javac -source 1.4 -target 1.4 compbug.java

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should behave as the 1.4 compiler and emit a compile-time error:

compbug.java:5: cannot resolve symbol
symbol  : method contains (java.lang.String)
location: class java.lang.String
                System.out.println("javac".contains("java"));

ACTUAL -
No compile-time error or warning messages using "javac -source 1.4 -target 1.4" (although one is left with a class file that is unusable in 1.4).

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No errors messages during compile-time, but one receives a runtime error in 1.4:

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.String.contains(Ljava/lang/CharSequence;)Z
        at compbug.main(compbug.java:5)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class compbug
{
	public static void main(String [] args)
	{
		System.out.println("javac".contains("java"));
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Apparently one must manually check all APIs to determine which are listed as "since 1.5" in the documentation.

Of course one could always use the 1.4 compiler to generate 1.4 class files, but that seems to acknowledge the relative uselessness of the "-source" and "-target" options, and discourages developers from using the latest and greatest tools available.

Comments
EVALUATION The @since tags have no semantic meaning and are only there for documentation purposes. If you're targeting 1.4.2, we recommend using JDK 1.4.2. However, if you still wish to run a newer compiler, you must -Xbootclasspath/:path option of javac to use rt.jar of the older release.
07-10-2005