How to use an older version of Java (not the default) with a Maven project on NetBeans 8

I need to use Java 6, but NetBeans doesn't use it even after all the configurations I've done. It keeps running Maven with Default, which is Java 8 for my IDE. The same configuration works well on Eclipse, so I don't understand what I am doing wrong.

I don't want to define the compiler in the nb-configuration.xml file as I will need to commit the file. I would expect NetBeans to get this from the POM.

General information:

  • I am using NetBeans 8.0.1 (completely updated), running Java 8
  • NetBeans 8 needs JDK 7+ to work
  • Using Maven 3.2.3 but built in (Maven 3.0.5 doesn't work either)
  • All Maven plugins updated

To reproduce, simply create any type of Maven project in NetBeans. In my case, I tried using Java, Web and EJB, but none worked.

The following figure shows that the JDK is correctly added to the IDE.

Tools> Java Platforms

Added JDK 1.6.

enter image description here

The POM configs I've tried:

Properties

<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>

      

Compiler plugin

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

      

Enforcer plugin

Forcing Java 6 implementation gives the following error:

Detected JDK Version: 1.8.0-25 is not in the allowed range [1.6,1.7).
------------------------------------------------------------------------
BUILD FAILURE

      

Operator configuration:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.3.1</version>
        <executions>
            <execution>
                <id>enforce-versions</id>
                <goals>
                    <goal>enforce</goal>
                </goals>
                <configuration>
                    <rules>
                        <requireJavaVersion>
                            <version>[1.6,1.7)</version>
                        </requireJavaVersion>
                    </rules>
                </configuration>
            </execution>
        </executions>
    </plugin>

      

Test code for Java 6

The following code shouldn't compile:

import java.nio.file.Files;
import java.time.LocalTime;

public class JavaVersionChecker {

    public static void main(String[] args) {

        System.out.println(Files.class); // Java 7 API
        System.out.println("Time: " + LocalTime.now()); // Java 8 API

        // Print Java version
        System.out.println(System.getProperty("java.version"));
    }

}

      

Test output

Compiled test code with Java 7 and 8 APIs, but only Java 6 should be accepted.

------------------------------------------------------------------------
Building java6_project 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ java6_project ---
class java.nio.file.Files
Time: 19:24:05.997
1.8.0_25
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 0.610 s
Finished at: 2014-11-21T19:24:06-02:00
Final Memory: 6M/123M
------------------------------------------------------------------------

      

JDK 6 should be detected by the IDE

enter image description here

+3


source to share


2 answers


As suggested in the comments, I did the job of configuring using the hint property , which is also well documented in the generated nb-configuration.xml file.

Config added to pom.xml:

<properties>
    <netbeans.hint.jdkPlatform>JDK_1.6</netbeans.hint.jdkPlatform>
</properties>

      



As far as I understand, the compilation version for NetBeans should be set with a proprietary parameter which is very easy to configure.

Useful information about nb-configuration.xml:

Properties that affect different parts of the development environment, especially code formatting and the like. You can copy and paste the single properties into the pom.xml file and the IDE will pick them up. That multiuser projects can use the same settings (useful for eg formatting rules). Any value specified here will override the value of the pom.xml file, but only apply to the current project.

+3


source


This error was caused by an old entry in nb-configuration.xml

Just adding this property to the pom.xml as suggested by @BonanzaOne did not work for me.

By changing

<netbeans.hint.jdkPlatform>JDK_1.6.0_34</netbeans.hint.jdkPlatform>

      



to

<netbeans.hint.jdkPlatform>JDK_1.6</netbeans.hint.jdkPlatform>

      

directly in nb-configuration.xml, Netbeans will automatically pick the existing JDK 1.6.

0


source







All Articles