对 Eclipse 中的不同项目同时使用 jdk 1.6 和 jdk 1.7

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

Using both jdk 1.6 and jdk 1.7 for different projects in Eclipse

eclipsejavajdk1.6

提问by Joey

I am using Eclipse with JDK 1.7. My previous projects are developed in java 7 and JDK 1.7. Now, I want to build a new project under JDK 1.6 in Eclipse. Is it possible to do that so that some projects are based on JDK 1.7 and some others are based on JDK 1.6 without conflicts?

我将 Eclipse 与 JDK 1.7 一起使用。我以前的项目是用 java 7 和 JDK 1.7 开发的。现在,我想在 Eclipse 中在 JDK 1.6 下构建一个新项目。是否可以这样做,以便某些项目基于 JDK 1.7 而其他一些项目基于 JDK 1.6 而不会发生冲突?

If this can be done, please let me know how to do it step by step.

如果这可以做到,请让我知道如何一步一步地做到这一点。

Update: My default JDK is 1.7. I created a new project that is based on jdk1.7 by default. And then I go to the property of this project, and then java compiler, then change the compliance settings as the image shows below. But there is some error occurred. What could be the reason causes this error?

更新:我的默认 JDK 是 1.7。我新建了一个默认基于jdk1.7的项目。然后我去这个项目的属性,然后java编译器,然后更改合规性设置,如下图所示。但是出现了一些错误。导致此错误的原因可能是什么?

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

The following is the "problem" view

以下是“问题”视图

enter image description here

在此处输入图片说明

After some research, I have solved this problem. please see the reference about how to fix the facet version.

经过一番研究,我已经解决了这个问题。请参阅有关如何修复 facet 版本的参考

采纳答案by Ted Hopp

You can set overall workspace properties to use one version of Java and set individual project properties to use another.

您可以将整体工作区属性设置为使用一个版本的 Java,并将单个项目属性设置为使用另一个版本。

  • To set overall workspace properties, click on Window> Preferences, then navigate to Java> Compiler.
  • To set project properties, right-click on the project and select Properties. Navigate to Java Compilerand check "Enable project specific settings"
  • 要设置整体工作区属性,请单击Window> Preferences,然后导航到Java> Compiler
  • 要设置项目属性,请右键单击该项目并选择Properties。导航到Java Compiler并选中“启用项目特定设置”

You can choose from among the installed JREs for either setting. To tell Eclipse about a newly installed JRE, you can open the workspace preferences and navigate to Java> Installed JREsand then click the "Add" button.

您可以为任一设置从已安装的 JRE 中进行选择。要将新安装的 JRE 告知 Eclipse,您可以打开工作区首选项并导航到Java>已安装的 JRE,然后单击“添加”按钮。

回答by Richard Sitze

This can be done for an Eclipse Run Configuration. You may define multiple such configurations, each of which will reference a "main class" in one of your projects.

这可以针对 Eclipse 运行配置完成。您可以定义多个这样的配置,每个配置都将引用您的一个项目中的一个“主类”。

  1. In Window -> Preferences -> Java -> Installed JREsdefine a location for each JRE/JDK.

  2. For a given Eclipse project:

    • right click on the project, select Properties -> Java Compiler, override and set compiliance levelsettings.

    • Check Use compliance from ..., visit the Java Build Pathand be sure you have a JRE Library set that matches the desired compliance level.

  3. For a given Eclipse Run Configuration: Run -> Run Configurations... -> Java Application, select an old or create a new configuration, select the JREtab and specify the Runtime JRE.

  1. Window -> Preferences -> Java -> Installed JREs 中为每个 JRE/JDK 定义一个位置。

  2. 对于给定的 Eclipse 项目:

    • 右键单击项目,选择Properties -> Java Compiler,覆盖并设置兼容级别设置。

    • 选中Use compliance from ...,访问Java Build Path并确保您拥有符合所需合规性级别的 JRE 库集。

  3. 对于给定的 Eclipse 运行配置:Run -> Run Configurations... -> Java Application,选择旧配置或创建新配置,选择JRE选项卡并指定Runtime JRE

回答by rocketboy

You can change the JDK used by project under Project properties(alt+enter)-> Installed JREs. Also Debug Configurationis your friend. You can set what JDK to use when launching/running an app/program in Eclipse.

您可以在 Project properties( alt+enter)-> Installed JREs下更改项目使用的 JDK 。同时调试配置是你的朋友。您可以设置在 Eclipse 中启动/运行应用程序/程序时要使用的 JDK。