是否可以使用 JDK 1.6 构建 Java 代码以在 JRE 1.5 上运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1927594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:30:46  来源:igfitidea点击:

Is is possible to build java code using JDK 1.6 to run on JRE 1.5?

javajava-6java-5

提问by amit

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

使用 JDK 1.6 编译时,是否可以选择构建在 JRE 1.5 上运行的 Java 代码?

PS: I am new to Java.

PS:我是 Java 新手。

采纳答案by Vincent Ramdhanie

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.

如果你用 1.6 编译你的代码,那么它就不会在 1.5 上运行。如果您希望它在 1.5 中运行,那么您可以使用 1.5 编译代码,它可以在两者上运行。

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.

尝试用 1.5 编译,如果有错误,则发布它们。它不会在 1.5 上编译的唯一方法是在代码中使用特定的 1.6 增强功能。



To answer the real question.

回答真正的问题。

  javac -target 1.5

See herefor more details.

请参阅此处了解更多详情。

回答by Mark Mayo

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

这完全取决于您使用的 API。Swing、Instrumentation、JConsole 等内容会随着时间而改变。

If you try:

如果你试试:

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

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

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.

当然,您需要在 1.5 下编译它才能让它与该 JRE 一起运行。

回答by Alistair Sutherland

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

看看 javac 的“-source”和“-target”选项:

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

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.

-源码发布

指定接受的源代码版本。允许使用以下发布值:

  • 1.3 编译器不支持断言、泛型或 JDK 1.3 之后引入的其他语言特性。
  • 1.4 编译器接受包含断言的代码,这是在 JDK 1.4 中引入的。
  • 1.5 编译器接受包含泛型和 JDK 5 中引入的其他语言特性的代码。
  • 5 1.5 的同义词。
  • 1.6 这是默认值。Java SE 6 中没有引入任何语言更改。但是,源文件中的编码错误现在报告为错误,而不是以前的警告。
  • 6 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.

-目标版本

生成以指定版本的 VM 为目标的类文件。类文件将在指定的目标和更高版本上运行,但不会在 VM 的早期版本上运行。有效目标是 1.1 1.2 1.3 1.4 1.5(也是 5)和 1.6(也是 6)。

-target 的默认值取决于 -source 的值: - 如果未指定 -source,则 -target 的值为 1.6

  • 如果 -source 为 1.2,则 -target 的值为 1.4
  • 如果 -source 为 1.3,则 -target 的值为 1.4
  • 对于 -source 的所有其他值,-target 的值就是 -source 的值。

回答by OliBlogger

Yes, you can. See http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.htmland 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.

是的你可以。请参阅http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html并查找“交叉编译选项”部分。简而言之,您需要为 javac 指定 -target=1.5。当然,Ant 也支持这个标志。