New javac warning for setting an older source without bootclasspath

To use javac from JDK N to cross-compiler to an older platform version, the correct practice is to:

  • Use the older -source setting.
  • Set the bootclasspath to compile against the rt.jar (or equivalent) for the older platform.

If the second step is not taken, javac will dutifully use the old language rules combined with new libraries, which can result in class files that do not work on the older platform since references to non-existent methods can get included.

Thanks to work by Jon Gibbons, in JDK 7 build 121 and later javac detects and warns about this suspicious situation; for example:

$ javac -source 6 HelloWorld.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6

One way to address the warning is to set the bootclasspath. If that is inappropriate, the warning can be disabled with a new suboption within the -Xlint family, -Xlint:-options.

With this change, a likely problematic combination of options to javac that can lead to subtle build errors are diagnosed by the compiler and can easily by either directly addressed, or documented as part of the build process via the new -Xlint suboption.

Comments:

I like the small and concise snippets of information about new features.

It's a great way to keep up to spped with the jdk as well as learn aspects of the language which are shielded from by using certain IDEs

Is there any other interesting sources of information (blogs, announcements etc..) you know of with a similar format

Posted by whatsthebeef on December 22, 2010 at 12:37 AM PST #

@whatsthebeef,

Besides the blogs.sun.com tag stream for JDK 7:
http://blogs.sun.com/main/tags/jdk7

Alex Miller has a feed of JDK 7 links:
http://java7.tumblr.com/

And many of Dalibor's OpenJDK tweets related to JDK 7:
http://twitter.com/openjdk

Posted by Joe Darcy on December 22, 2010 at 11:32 PM PST #

Isn't this setting misleading? Setting the bootclasspath will work when a new method is added, but if a new class has been added (e.g. StringBuilder), it will still be found from the newer rt.jar. I feel that doing this instead of building on the actual older JDK gives a false sense of security, without ensuring compatibility.

Posted by Jevgeni Kabanov on December 24, 2010 at 06:47 PM PST #

@Jevgeni,

Setting the bootclasspath option of javac is exactly the option which prevents javac from looking for (and finding) new classes, such as StringBuilder in the rt.jar (or equivalent) on the current platform.

Building on an older JDK works too of course.

Posted by Joe Darcy on December 26, 2010 at 11:59 PM PST #

Post a Comment:
Comments are closed for this entry.
About

darcy

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
News

No bookmarks in folder

Blogroll