• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compile on Java 6 for Java 5  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If I have Java 6 installed, but I want to ensure my built classes will work with a JRE that is only Java 5, is there any automated check available?

I am aware that there are -source and -target arguments to javac, and know these check that language features (e.g. generics) are appropriate to the chosen version. But do the checks also ensure that one hasn't used any classes that won't be available in the specified version?
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was recent discussion about a similar topic here. you should find more details about compiler options here.

[edit]Fix broken link[/edit]
[ April 17, 2007: Message edited by: Campbell Ritchie ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the short answer is no - one cannot check at compile time whether one has inadvertently used classes that are Java 6-specific. Correct?
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]I made a mistake preparing this post See my following post[/edit]

Don't know. Will try it.

I tried to compile this:-and got this:-
[Campbell@dhcppc0 DATAWRITE]$ gedit J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ javac -d . -target 5 J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ java trivia.J5CrashTest
[Campbell@dhcppc0 DATAWRITE]$ javac -d . -target 1.5 J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ java trivia.J5CrashTest
[Campbell@dhcppc0 DATAWRITE]$ gedit J5CrashTest.java
Console and System.console() were introduced in J6. As you see, it runs and doesn't print anything, nor does it throw an Exception.

So it would appear you can't tell at compile-time whether you are using J5 compatible features.


Maybe somebody else knows a better way?
[ April 17, 2007: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might have worked better if I had put anything inside the main method. Try adding this:-Then you get this output:-
[Campbell@dhcppc0 DATAWRITE]$ gedit J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ javac -d . -target 1.5 J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ java trivia.J5CrashTest
java.io.Console@14318bb

So it seems it compiles the J6 features in J5 compatibility mode. I am using J6, so I can't tell what would happen if you used that on a computer with J5 only on.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ought to learn to read . . .

There is also a -source option, but it seems to compile myJ5CrashTest.java class. I cannot get it to crash however hard I try!
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try again with -source 1.4:
[Campbell@dhcppc0 DATAWRITE]$ javac -d . -source 5 -target 1.5 J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ javac -d . -source 1.5 -target 1.5 J5CrashTest.java
[Campbell@dhcppc0 DATAWRITE]$ javac -d . -source 1.4 -target 1.5 J5CrashTest.java
J5CrashTest.java:4: static import declarations are not supported in -source 1.4
(use -source 5 or higher to enable static import declarations)
import static java.lang.System.*;
^
1 error
[Campbell@dhcppc0 DATAWRITE]$
So it will recognise the absence of syntactic features but not of a class. Maybe the -target option will compile J6 code so it runs on a J5 machine.

Will try again later.

CR
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try again with another PC using WinXP and Java 5:-Code: Exactly the same as I showed you earlier, but with the main method actually filled in!

CR

Anybody else got any ideas?
[edit]Minor spelling corrections[/edit]
[ April 17, 2007: Message edited by: Campbell Ritchie ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "-source" and "-target" switches do not check if you are using a standard API class that is not in the release you are compiling for.

The only thing these switches do is check for Java language syntax for the specified version, and the class file that's written is compatible with the specified Java version.

In other words no, it is not "safe" to compile your Java source with JDK 6 if you want to run it on Java 5.0 or older. If you use a Java 6.0 class, interface or method, you will get a NoClassDefFoundError, NoSuchMethodError or something similar at runtime on the older Java version, as Campbell has discovered.
[ April 17, 2007: Message edited by: Jesper Young ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!