I maintain a Java application which I have always compiled using JDK 1.6 before distributing it as this is the minimum version that my application requires - I don't use any newer features. I don't see the point of compiling it in a later version or older JREs won't be able to run it (i.e. users on Mac OSX which use Java 1.6).

Now, with security in the news recently with respect to Java and with bug fixes in later versions of the JDK, is it better to cross compile it using the latest JDK available using the following syntax:

javac -Xlint -source 1.6 -target 1.6 -bootclasspath jre6/lib/rt.jar MyClass.java

This produces a Class file which runs fine under JRE 1.6, but are there any benefits over using JDK 1.6 in the first place? Am I taking advantage of any optimisations in newer JDKs or am I generating identical bytecode?

有帮助吗?

解决方案

There are several points you should consider:

  1. The bytecode of string concatenation changed several times. It is no longer a pitfall for bad performance. Yet i'm not sure if this comes from the refactoring of the bytecode representation or from changing the VM.
  2. An old (system) JDK/JRE may open security risks on the development machine. If the developer is using an outdated JDK/JRE he may be vulnerable to malicious software he is executing.
  3. An outdated java version on client side exposes the user to security risks. If you allow your user to use an old Java version they will do it. This may be a security risk.
  4. Java 6 is no longer publicy supported. The security problem is that a user is neither informed that his java version should be updated nor is he forced to do it. Keep in mind that Java is in the top3 list of security vulnerabilities.
  5. You can use Java 1.7 features while compiling for 1.6 target. Using -source 1.7 and -target 1.6 you can use language features like int i = 0b011
  6. Tools shipped with your JDK get updated. There maybe new tools for developing java application, e.g. at some point java VisualVM was added to the JDK.

Looking at that list i think the pros of using a current JDK outweighs the pros of using an old one.

EDIT: I investigated a little bit and i suggest now that you install the JDK which suits your target version. You need the JDK anyway when you are cross compiling because of the bootstrap/ext classpath. See How to cross-compile for older platform versions for further information. This site also states that there are differences in compilation between different JDKs (second to last paragraph) and suggests to use the JDK relevant to your target version (last paragraph). Also relevant may be the javac documentation.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top