Is it possible to compile class files with the Jav

2020-02-10 13:02发布

问题:

Since the public Java 6 SE JRE is getting closer to it's EOL (Nov '12), I'm considering porting my projects from Java 6 to Java 7. This would'nt be a big deal, if Apple would provide a Java 7 JRE for Mac OS X. But since Apple isn't willing to do so, I still have to support users which only have a Java 6 JRE.

Is there a way to compile Java 6 compatible binaries (class files) with the Java 7 javac? Certainly I'm aware that I can't use the new Java 7 features when doing so.

Thanks in anticiption!

回答1:

It depends. If your program doesn't use the new Java 7 language extensions, then you can run the Java compiler with the -source 1.6 and -target 1.6 options. But if you use Java 7 language extensions then -source 1.6 will result in compilation errors.

Certainly I'm aware that I can't use the new Java 7 features when doing so.

That includes Java 7 language features ... and dependencies on Java 7 changes to the standard class library APIs. Also be aware that there are small number of behavioural differences (aka API bug fixes) that may cause code to run differently on Java 6 and Java 7. They should be described in the Java 6 to Java 7 transition document.


UPDATE - This probably no longer an issue for you anyway, because Oracle have released Java 7 for Mac OSX.



回答2:

i have jdk6 installed. if you check the man page of javac:

Cross-Compilation Options
          By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compil‐
          ing, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use -bootclasspath
          and -extdirs when cross-compiling; see Cross-Compilation Example below.

         -target version
            Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier
            versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).


回答3:

Yes, but in some cases no. In java 1.6 they didn't have the try with resources, switch with strings, or multi catch statements etc. So those parts of the program will not compile. But the idea of java is compile once, run everywhere; so code can work on old JVMs



回答4:

Stephen C's answer is correct, but not complete. Your Java 7 programs won't compile in Java 6 if they use Java 7 language features, but be warned subtle other bugs can still occur with one developer coding in Java 6 and another compiling Java 7.

Take for example java.sql.Driver. In Java 7, the interface gained an additional method.

Java 7 Developer This developer implements the Driver interface and uses the 'Override' annotation on the implemented additional Driver method. The program compiles fine as a Java 6 program because the class that the Java 6 compiler sees does have that method and the code gets checked in. Compiling the program as Java 6 does not mean that Java 6 compiler will automatically switch to use Java 6 source code!

Java 6 Developer The Java 6 developer attempts to build the code the Java 7 developer committed and gets a compilation error even though the Java 7 developer was not implementing any Java 7 language constructs.

Consequently, even though you could compile it as Java 6, I would recommend not doing this.