2015-08-25 75 views
2

我已經通過點擊File->New->Other->Maven->New Project在eclipse中創建了新的maven java項目。我發現項目使用Java 1.5。在我的PC中只存在java 1.4和java 8.我需要使用java 1.4 JDK編譯項目。我轉到Project-> Properties-> JRE System Library並更改爲java 1.4。當我運行主類我有錯誤:更改maven eclipse項目中的java SDK版本

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) 

如何使項目java 1.4兼容?

+0

爲什麼不使用1.5 maven的編譯器插件? –

+1

您需要使用[以下設置]在maven中定義源.java文件和目標.class文件版本(https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source -and-target.html)在你的'pom.xml'文件中。 – A4L

回答

3

首先,我會定義一個屬性來控制值。喜歡的東西,

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

,然後添加一個構建節,像

<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> 
0

通過這種方式指定目標 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> 
0

通常,IDE使用maven pom.xml文件作爲項目配置的來源。在IDE中更改編譯器設置並不總是對maven構建產生影響。保持項目始終可以通過maven管理的最好方法是編輯pom.xml文件並指示IDE與maven同步。

配置在pom.xml

<build> 

<plugins> 
    <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>1.5</source> 
      <target>1.4</target> 
     </configuration> 
    </plugin> 
</plugins> 
... 
  • 或者設置這些屬性(總是在POM)
<properties> 
<maven.compiler.source>1.5</maven.compiler.source> 
<maven.compiler.target>1.4</maven.compiler.target> 
</properties> 
相關問題