如何为以前的 Java 版本构建 jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4393249/
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-14 16:56:36  来源:igfitidea点击:

How can I build a jar for a previous Java version?

javaeclipsejavac

提问by codeObserver

I am trying to compile my code and run it on a different server. The problem is my JRE version is Java version "1.6.0_13" and that on the server is Java version "1.4.2".

我正在尝试编译我的代码并在不同的服务器上运行它。问题是我的 JRE 版本是 Java 版本“1.6.0_13”,而服务器上的版本是 Java 版本“1.4.2”。

This gives me "unrecognized class file version" exception when I try to run the jar on the server. I cannot compile my code on the server because of various dependencies I will have to set up.

当我尝试在服务器上运行 jar 时,这给了我“无法识别的类文件版本”异常。我无法在服务器上编译我的代码,因为我必须设置各种依赖项。

So I need to know if there is a way to "use Eclipse" [this is easier than command line since it takes care of dependencies] and compile my stuff using 1.4.2 instead 1.6.0_13. Do I have to uninstall my JRE from my machine, then reinstall previous version and then compile or is there an elegant and cleaner way?

所以我需要知道是否有一种方法可以“使用 Eclipse”[这比命令行更容易,因为它处理依赖项]并使用 1.4.2 而不是 1.6.0_13 编译我的东西。我是否必须从我的机器上卸载我的 JRE,然后重新安装以前的版本,然后编译,还是有一种优雅和干净的方式?

采纳答案by Yishai

You should install 1.4.2 along side 1.6 on your machine, and configure that eclipse project to use the 1.4.2 JDK. Other projects may be configured to use the 1.6.

您应该在您的机器上安装 1.4.2 和 1.6,并将该 eclipse 项目配置为使用 1.4.2 JDK。其他项目可以配置为使用 1.6。

Technically you can compile to 1.4.2 compatibility with JDK 1.6, but odds are you will run into library problems, so it is not generally worth doing.

从技术上讲,您可以编译为与 JDK 1.6 兼容的 1.4.2,但很可能会遇到库问题,因此通常不值得这样做。

回答by rustyx

You can configure the Source and Target Java version in Eclipse.

您可以在 Eclipse 中配置源和目标 Java 版本。

Also in command line you can compile for a certain (older) Java version using -source and -target parameters:

同样在命令行中,您可以使用 -source 和 -target 参数编译某个(较旧的)Java 版本:

javac -source 1.4 -target 1.4

回答by Olivier Faucheux

You can compile for java 1.5 with the parameters

您可以使用参数为 java 1.5 编译

javac -source 1.5 -target 1.5 -bootclasspath "C:\Program Files (x86)\Java\jre1.5.0_21\lib\rt.jar"

Do not forget the bootclasspath, or you will have problems with functions like String.isEmpty() which don't exist in the java 1.5.

不要忘记引导类路径,否则你会遇到像 String.isEmpty() 这样在 java 1.5 中不存在的函数的问题。

回答by ROMANIA_engineer

Eclipse Mars (4.5.1)

日食火星 (4.5.1)



Change JDK compliance

更改 JDK 合规性

Window> Preferences> Java> Compiler> JDK Compliance > change Compiler compliance level:

窗口>首选项> Java>编译器> JDK 合规性 > 更改编译器合规性级别:

Compiler compliance level

编译器合规级别

If you want to change other details, you can uncheck "Use default compliance settings".

如果要更改其他详细信息,可以取消选中“使用默认合规性设置”。

Press OKand in the "Compiler Settings Changed" window choose:

OK并在“Compiler Settings Changed”窗口中选择:

  • Yesif you want to build all projects from the workspace:

    Compiler Settings Changed

  • Noif you want to build only one/some project(s) later

    • Select the project (click on the left side - E.g.: in the Package Explorer) > Project> Build Project
  • 是的,如果您想从工作区构建所有项目:

    编译器设置已更改

  • 如果您以后只想构建一个/一些项目,则

    • 选择项目(单击左侧 - 例如:在包资源管理器中)>项目>构建项目


Export jar

出口罐

Right clickon the project (from the left side - E.g.: in the Package Explorer) > Export...> type "jar" to filter the options you have > choose what you want (probably JAR fileor Runnable JAR file) > Next > add the details > Finish

右键单击项目(从左侧 - 例如:在包资源管理器中)>导出...> 键入“jar”以过滤您拥有的选项> 选择您想要的(可能是JAR 文件Runnable JAR 文件)> 下一步> 添加详细信息 > 完成