United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6226858 : NoSuchMethodError in BigDecimal when compiling with 1.5 targetted for 1.4

Details
Type:
Bug
Submit Date:
2005-02-08
Status:
Closed
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2005-02-08
Component:
tools
OS:
linux
Sub-Component:
javac
CPU:
x86
Priority:
P3
Resolution:
Not an Issue
Affected Versions:
5.0
Fixed Versions:

Related Reports
Relates:
Relates:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
Both :
java version "1.4.2_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_07-b05)
Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode)
And
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode)



ADDITIONAL OS VERSION INFORMATION :
Linux dub-280-41 2.6.10-gentoo-r4 #1 SMP Mon Jan 10 12:03:01 GMT 2005 i686 Intel(R) Pentium(R) 4 CPU 3.00GHz GenuineIntel GNU/Linux


A DESCRIPTION OF THE PROBLEM :
I was looking into migrating from JDK1.4 to JDK1.5, and given the code (in the example code section)

when I compile this on JDK1.4, it compiles and runs correctly
However, when I compile it as:
/opt/sun-jdk-1.5.0.01/bin/javac -source 1.4 Test.java
And run it (using the 1.4 JVM), I get the following error at runtime:
Exception in thread "main" java.lang.NoSuchMethodError: java.math.BigDecimal.<init>(I)V


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the example source using JDK1.4, run -- works correctly
Compile the example source using JDK1.5, and run against the 1.5 JVM -- works correctly
Compile the example source using JDK1.5, and run against the 1.4 JVM -- fails with NoSuchMethodError

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Should work on all releases.
ACTUAL -
NoSuchMethodError

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NoSuchMethodError: java.math.BigDecimal.<init>(I)V
        at Test.main(Test.java:6)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.math.BigDecimal;

public class Test
{
        public static void main(String[] args)
        {
                BigDecimal dec = new BigDecimal(1);
        }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Change the code to
                BigDecimal dec = new BigDecimal((double) 1);
###@###.### 2005-2-08 21:09:42 GMT

                                    

Comments
EVALUATION

As part of jsr 13, a new constructor, BigDecimal(int val), was added to BigDecimal in JDK 5.  Therefore, in 1.4 and earlier

new BigDecimal(1)

in the source resolves to BigDecimal(double) while in 1.5 it resolves to BigDecimal(int), which is not present in earlier JDKs.  The -source flag of javac only restricts the constructs in the source languge (e.g. assert, generics); it does *not* restrict or filter the contents of the libraries.  Therefore, in JDK5 if you compile with "javac -source 1.4" you will still be compiling against the 1.5 version of the libraries.  Therefore, in cases like the one in this bug, code compiled at a lower language level might not be able to be used with an ealier release supporting that language level.

To match both the language and libraries of an ealier jdk, use -source and the -bootclasspath flag to specify the earlier rt.jar as well.  Of course the ealier jdk could be used too.

Closing as not a bug.

###@###.### 2005-2-08 21:43:19 GMT
                                     
2005-02-08



Hardware and Software, Engineered to Work Together