简体   繁体   English

Eclipse中JRE系统库的概念

[英]concept of JRE system library in eclipse

I have been using eclipse for years but this never occurred to me. 我已经使用eclipse多年了,但这对我而言从未发生过。 I have JDK7 installed on my system and I use this as workspace JRE. 我的系统上安装了JDK7,并将其用作工作区JRE。 however, When I include the following definition in maven project, The library section shows java library as [JavaSE 1.6] as shown in the below image. 但是,当我在maven项目中包含以下定义时,库部分将Java库显示为[JavaSE 1.6],如下图所示。

在此输入图像描述

<build>
     <plugins>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                   <source>1.6</source>
                   <target>1.6</target>
                </configuration>
        </plugin>
    </plugins>
</build>

I understand the concept of source and target configuration. 我了解源和目标配置的概念。 But, I'm just not understanding the way JDK7 libraries acts as JRE6(Or does it?). 但是,我只是不了解JDK7库充当JRE6的方式(或者是吗?)。 Could anyone explain? 谁能解释?

I'm using Eclipse Mars with JDK 1.7 as work bench JRE 我将带有JDK 1.7的Eclipse Mars用作工作台JRE

That is the minimum configuration version of java required for your project to run(Help-->About Eclipse-->Installation details-->Configuration) 那是您的项目运行所需的Java的最低配置版本(帮助->关于Eclipse->安装详细信息->配置)

Refer the below snip, 请参考以下片段,

Even though i user 1.8, the min version reqd is 1.6 即使我使用1.8,最低版本要求是1.6 在此输入图像描述

There is no protection against using the wrong APIs when setting your configuration to an older JRE than what you are using to build. 在将配置设置为比要构建的版本更旧的JRE时,无法避免使用错误的API。

From the manual 手册

Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. 注意:仅设置目标选项不能保证您的代码实际在具有指定版本的JRE上运行。 The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. 陷阱是仅在更高版本的JRE中存在的API的意外使用,这将使您的代码在运行时因链接错误而失败。 To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs. 为避免此问题,您可以将编译器的引导类路径配置为与目标JRE匹配,或者使用Animal Sniffer Maven插件来验证您的代码未使用意外的API。

But to answer your question: 但是要回答你的问题:

But, I'm just not understanding the way JDK7 libraries acts as JRE6. 但是,我只是不了解JDK7库充当JRE6的方式。 Could anyone explain? 谁能解释?

It doesn't , at least not really. 并非如此 ,至少不是真的。 If for example you have (only) JRE 8 installed but have your project set at JRE 7 you can write this line of code without compile error as it is legal JRE 7 Java, just using a method ( join ) added in JRE 8's runtime. 例如,如果您(仅)安装了JRE 8,但您在JRE 7上设置了项目,则可以编写此行代码而不会产生编译错误,因为这是合法的JRE 7 Java,只需使用在JRE 8的运行时中添加的方法( join )。

String join = String.join("", "a", "b");

When you run it with JRE 7 you will get this at runtime: 当您使用JRE 7运行它时,您将在运行时得到它:

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.String.join(Ljava/lang/CharSequence;[Ljava/lang/CharSequence;)Ljava/lang/String;
  at snippet.Snippet.main(Snippet.java:5)

Therefore, if you really want to ensure compliance, build and develop with the right JRE installed on your system. 因此,如果您确实想确保合规性,请在系统上安装正确的JRE进行构建和开发。

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

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