简体   繁体   English

如何使用旧版本的java 1.6运行.jar 1.7程序?

[英]how to run a .jar 1.7 program with an older version java 1.6?

我有一个在1.7版本java中运行良好的code.jar,但是当使用另一台带有1.6版本的linux的计算机时,它给我一个错误,我怎么能在1.6版本中运行它?

Use the cross-compilation options when 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 . 如果在大多数其他回复中仅按照建议指定-target选项,则1.7 SDK将发出有关-bootclasspath的警告。 That option requires an rt.jar of the target JRE version (note not JDK). 该选项需要目标JRE版本的rt.jar (注意不是 JDK)。 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 . 它允许编译器检查引用核心Java SE的代码的类,方法和属性是否实际存在于rt.jar This is very important to ensure compatibility. 这对于确保兼容性非常重要。

如果代码未使用较旧的Java兼容性标志进行编译,则无法在1.6上运行它。

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. 现在右键单击您的项目 - > Properties,单击Java Compiler,取消选中“enable project specific settings”,单击“Configure Workspace Settings”,然后选择1.6作为编译器合规级别。

Do "the thing" to export your project again, the new jar should work. 做“事情”再次导出你的项目,新的jar应该工作。

Hope this helps. 希望这可以帮助。

I don't think you can run higher version class with lower version. 我认为您不能使用较低版本运行更高版本的类。

Another approach may be while compiling set target version as 1.6. 另一种方法可能是将目标版本设置为1.6。

Example: 例:

javac yourFile.java -target 1.6

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. 如果您转到项目并右键单击以访问属性,则会出现一个名为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. 如果启用项目特定属性并取消选中JDK合规性下的复选框,则可以将源兼容性和.class文件兼容性更改为1.6。

This should fix your problem and the generated .jar file should work on both devices. 这应该可以解决您的问题,并且生成的.jar文件应该可以在两个设备上运行。 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. 我通过将文件从1.8移动到1.7并在具有OpenJDK 1.7的Raspberry Pi上进行测试来测试这一点。 I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM