3

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:

  • I am using NetBeans 8.0.1 (fully updated) running on Java 8
  • NetBeans 8 needs JDK 7+ in order to work
  • Using Maven 3.2.3, but the embedded (Maven 3.0.5 didn't work as well)
  • All Maven plugins are up to date

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

16
  • please note that [1.6,) is satisfied with 1.8 as well. Commented Nov 22, 2014 at 3:44
  • also I'm not sure what you mean by "portable", is that "I don't want to do any IDE specific setup at all"? Then there is no solution to your problem. However there are ways to make the setup once and share it across your projects and coworkers (so that it's setup just once). If you read what is generated in nb-configuration.xml, it will give you a hint. Commented Nov 22, 2014 at 3:47
  • @mkleint For your fist comment, I edited the question with the right conf. tx. Commented Nov 22, 2014 at 13:36
  • @mkleint As for your second comment, plug-ins and other stuff are there for this purpose, to avoid binding configuration of IDE with the product. In Eclipse a simple property configuration solves all my problems for this question, and I wanna know why NetBeans is not working the same way. Commented Nov 22, 2014 at 13:39
  • @BonanzaOne In eclipse it works because of the m2e plugin but as far as I know NetBeans supports this feature too. In eclipse you have to run "update project configuration" to apply settings like this, maybe you need to do something similar in Netbeans. Commented Nov 24, 2014 at 14:12

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.