eclipse 如何使用旧版本的 java 1.6 运行 .jar 1.7 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12081128/
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
how to run a .jar 1.7 program with an older version java 1.6?
提问by user1475288
I have a code.jar running well in 1.7 version java, but when using another computer with linux with version 1.6 it is giving me an error, how can I run it in 1.6 version?
我有一个 code.jar 在 1.7 版 java 中运行良好,但是当使用另一台带有 1.6 版 linux 的计算机时,它给我一个错误,我如何在 1.6 版中运行它?
回答by Andrew Thompson
Use the cross-compilation optionswhen invoking javac
.
调用时使用交叉编译选项javac
。
If only the -target
option is specified as recommended in most other replies, the 1.7 SDK will issue a warning about -bootclasspath
. That option requires an rt.jar
of the target JRE version (note notJDK). It allows the compiler to check that the classes, methods and attributes of the code that reference core Java SE are actually present in the rt.jar
. This is very important to ensure compatibility.
如果仅按照-target
大多数其他回复中的推荐指定该选项,则 1.7 SDK 将发出关于-bootclasspath
. 该选项需要一个rt.jar
目标 JRE 版本(注意不是JDK)。它允许编译器检查引用核心 Java SE 的代码的类、方法和属性是否确实存在于rt.jar
. 这对于确保兼容性非常重要。
回答by Dan D.
If the code is not compiled with an older Java compatibility flag, you won't be able to run it on 1.6.
如果代码不是用旧的 Java 兼容性标志编译的,您将无法在 1.6 上运行它。
回答by BZapper
Build the project using Java 6 in eclipse, you will need to download Java 1.6 JRE, then have eclipse point to 1.6 JRE, see: http://www.cleartoolkit.com/dokuwiki/doku.php?id=clearwiki:20.cleardatabuilder:01.setup:01.prerequisites
在eclipse中使用Java 6构建项目,需要下载Java 1.6 JRE,然后让eclipse指向1.6 JRE,参见:http: //www.cleartoolkit.com/dokuwiki/doku.php?id=clearwiki:20 。 cleardatabuilder:01.setup:01.prerequisites
Now right click on your project -> Properties, click Java Compiler, uncheck "enable project specific settings", click "Configure Workspace Settings" , and select 1.6 as your compiler compliance level.
现在右键单击您的项目 -> 属性,单击 Java 编译器,取消选中“启用项目特定设置”,单击“配置工作区设置”,然后选择 1.6 作为您的编译器合规性级别。
Do "the thing" to export your project again, the new jar should work.
做“事情”再次导出您的项目,新的 jar 应该可以工作。
Hope this helps.
希望这可以帮助。
回答by kosa
I don't think you can run higher version class with lower version.
我不认为你可以用较低版本运行较高版本的类。
Another approach may be while compiling set target versionas 1.6.
另一种方法可能是将设置的目标版本编译为 1.6。
Example:
例子:
javac yourFile.java -target 1.6
回答by Radar6255
I have found a solution within Eclipse!
我在 Eclipse 中找到了解决方案!
If you go to your project and right click to access the properties, there is a tab called Java Compiler. If you enable project specific properties and unclick the check box under JDK compliance, you can change the source compatibility and .class files compatibility to be 1.6.
如果您转到您的项目并右键单击以访问属性,则会有一个名为 Java Compiler 的选项卡。如果启用项目特定属性并取消选中 JDK 合规性下的复选框,则可以将源兼容性和 .class 文件兼容性更改为 1.6。
This should fix your problem and the generated .jar file should work on both devices. I tested this by moving a file from 1.8 to 1.7 and testing on my Raspberry Pi that has OpenJDK 1.7 which worked great. I hope this helps.
这应该可以解决您的问题,并且生成的 .jar 文件应该可以在两个设备上运行。我通过将文件从 1.8 移动到 1.7 并在我的具有 OpenJDK 1.7 的 Raspberry Pi 上进行测试来测试它,它运行良好。我希望这有帮助。