English 中文(简体)
Is is possible to build java code using JDK 1.6 to run on JRE 1.5?
原标题:

Is there an option to build java code to run on JRE 1.5 when compiled using JDK 1.6?

PS: I am new to Java.

最佳回答

If you compile your code with 1.6 then it will not run on 1.5. If you want it to run in 1.5 then you can compile the code with 1.5 and it would be able to run on both.

Try compiling with 1.5 and if there are errors then post them. The only way it will not compile on 1.5 is if you use specific 1.6 enhancements in your code.


To answer the real question.

  javac -target 1.5

See here for more details.

问题回答

Yes, you can. See http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html and look for the section "Cross-Compilation Options". In short, you need to specify -target=1.5 to javac. Ant also supports this flag, of course.

Have a look at the javac "-source" and "-target" options:

http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html

-source release

Specifies the version of source code accepted. The following values for release are allowed:

  • 1.3 The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
  • 1.4 The compiler accepts code containing assertions, which were introduced in JDK 1.4.
  • 1.5 The compiler accepts code containing generics and other language features introduced in JDK 5.
  • 5 Synonym for 1.5.
  • 1.6 This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
  • 6 Synonym for 1.6.

+

-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).

The default for -target depends on the value of -source: - If -source is not specified, the value of -target is 1.6

  • If -source is 1.2, the value of -target is 1.4
  • If -source is 1.3, the value of -target is 1.4
  • For all other values of -source, the value of -target is the value of -source.

It all depends on what APIs you are using. Things like Swing, Instrumentation, JConsole etc change over time.

If you try:

http://www.coderanch.com/t/382318/Java-General/java/New-Features-Java

it has links to the pages indicating the differences between each of the last major versions, with:

http://java.sun.com/javase/6/webnotes/features.html

being a list of the changed/new features in the latest version.

Hopefully that ll give you some idea.

And of course then you ll need to compile it under 1.5 to get it to run with that JRE.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签