Evandro Pomatti
Evandro Pomatti

Reputation: 15144

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

I need to use Java 6 but NetBeans is not using it even after all the configurations I made. It keeps running Maven with the Default that is Java 8 for my IDE. The same configuration runs well on Eclipse so I don't get what I am doing wrong.

I don't want to define the compiler in the nb-configuration.xml since I would have to commit the file. I would expect NetBeans to get it right from the POM.

General information:

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

The following image shows that the JDK is properly added to the IDE.

Tools > Java Platforms

JDK 1.6 is added.

enter image description here

POM configurations that I have 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

Enforcing Java 6 gives the following error:

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

Configuration for the enforcer:

    <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 should not 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

The test code compile with Java 7 and 8 API, 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

Upvotes: 3

Views: 4242

Answers (2)

tak3shi
tak3shi

Reputation: 2415

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

Just adding this property to pom.xml as suggested by @BonanzaOne did not worked 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 picks the existing JDK 1.6 automaticaly.

Upvotes: 0

Evandro Pomatti
Evandro Pomatti

Reputation: 15144

As suggested by the comments I made the configuration work using a hint property, which is also very well documented in the generated nb-configuration.xml.

Configuration added to the pom.xml:

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

As I understand the compile version for NetBeans needs to be set by a proprietary parameter, which is very simple to set up.

Useful nb-configuration.xml details:

Properties that influence various parts of the IDE, 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 way multiple projects can share the same settings (useful for formatting rules for example). Any value defined here will override the pom.xml file value but is only applicable to the current project.

Upvotes: 3

Related Questions