简体   繁体   English

在Maven Eclipse项目中更改Java SDK版本

[英]Change java SDK version in maven eclipse project

I have created new maven java project in eclipse by clicking File->New->Other->Maven->New Project . 我通过单击File->New->Other->Maven->New Project在Eclipse中创建了新的Maven Java项目。 I found that projects is using java 1.5. 我发现项目正在使用Java 1.5。 In my PC exist only java 1.4 and java 8. I need to compile project using java 1.4 JDK. 在我的PC中,仅存在Java 1.4和Java8。我需要使用Java 1.4 JDK编译项目。 I go to Project->Properties-> JRE System Library and change to java 1.4. 我转到项目->属性-> JRE系统库,然后更改为Java 1.4。 When I run main class I have error: 运行主类时出现错误:

java.lang.UnsupportedClassVersionError: arr/ff (Unsupported major.minor version 49.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

How to make project java 1.4 compatible? 如何使项目Java 1.4兼容?

First, I'd define a property to control the value. 首先,我将定义一个属性来控制值。 Something like, 就像是,

<properties>
    <java.version>1.4</java.version>
</properties>

And then add a build stanza, like 然后添加一个构建节,例如

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <verbose>true</verbose>
                <fork>true</fork>
                <debug>false</debug>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Specify source and target Java version by this way: 通过这种方式指定目标 Java版本:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.5</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Usually IDEs use maven pom.xml file as a source of project configuration. 通常,IDE使用maven pom.xml文件作为项目配置的源。 Changing the compiler settings in the IDE not always has effect on the maven build. 在IDE中更改编译器设置并不总是会影响maven构建。 The best way to keep a project always manageable with maven is edit the pom.xml files and instruct the IDE to sync with maven. 保持项目始终可通过maven管理的最佳方法是编辑pom.xml文件,并指示IDE与maven同步。

Configure the maven compiler plugin in the pom.xml 在pom.xml中配置Maven编译器插件

<build>

<plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.5</source>
            <target>1.4</target>
        </configuration>
    </plugin>
</plugins>
...
  • OR Set these properties (always in the pom) 或设置这些属性(始终在pom中)
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.4</maven.compiler.target>
</properties>

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

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