var cannot be resolved to a type - java-10

Vinay Prajapati picture Vinay Prajapati · Mar 22, 2018 · Viewed 7.2k times · Source

I simply installed JDK 10 on my mac machine. Checked for the version:-

localhost:java-10 vinayprajapati$ java -version
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

Just to make sure that compiler used is also same version, I ran below:-

localhost:java-10 vinayprajapati$ javac -version
javac 10

I created a simple Maven project with following structure:-

localhost:java-10 vinayprajapati$ tree
.
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── practice
│   │               └── java_10
│   │                   └── App.java
│   └── test
│       └── java
│           └── com
│               └── practice
│                   └── java_10
│                       └── AppTest.java
└── target

My pom.xml is:-

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.practice</groupId>
    <artifactId>java-10</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>java-10</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <release>10</release>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>6.1</version> <!-- Use newer version of ASM -->
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

My App.java file is:-

package com.practice.java_10;

import java.util.ArrayList;

public class App {
    public static void main(String[] args) {
        var list = new ArrayList<String>(); 
        System.out.println("Hello Java 10! Shall I welcome you?");

    }
}

I ran mvn compile and mvn install and both worked successfully. Proof below:-

localhost:java-10 vinayprajapati$ mvn compile
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< com.practice:java-10 >------------------------
[INFO] Building java-10 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-10 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/vinayprajapati/Desktop/project/java-10/java-10/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ java-10 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/vinayprajapati/Desktop/project/java-10/java-10/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.823 s
[INFO] Finished at: 2018-03-23T01:49:13+05:30
[INFO] ------------------------------------------------------------------------
localhost:java-10 vinayprajapati$ mvn install
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< com.practice:java-10 >------------------------
[INFO] Building java-10 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-10 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/vinayprajapati/Desktop/project/java-10/java-10/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ java-10 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-10 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/vinayprajapati/Desktop/project/java-10/java-10/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ java-10 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/vinayprajapati/Desktop/project/java-10/java-10/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-10 ---
[INFO] Surefire report directory: /Users/vinayprajapati/Desktop/project/java-10/java-10/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.practice.java_10.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-10 ---
[INFO] Building jar: /Users/vinayprajapati/Desktop/project/java-10/java-10/target/java-10-0.0.1-SNAPSHOT.jar
[INFO] META-INF/maven/com.practice/java-10/pom.xml already added, skipping
[INFO] META-INF/maven/com.practice/java-10/pom.properties already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ java-10 ---
[INFO] Installing /Users/vinayprajapati/Desktop/project/java-10/java-10/target/java-10-0.0.1-SNAPSHOT.jar to /Users/vinayprajapati/.m2/repository/com/practice/java-10/0.0.1-SNAPSHOT/java-10-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/vinayprajapati/Desktop/project/java-10/java-10/pom.xml to /Users/vinayprajapati/.m2/repository/com/practice/java-10/0.0.1-SNAPSHOT/java-10-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.157 s
[INFO] Finished at: 2018-03-23T01:49:22+05:30
[INFO] ------------------------------------------------------------------------

When ran mvn exec:java -Dexec.mainClass="com.practice.java_10.App", I got error as below :-

localhost:java-10 vinayprajapati$ mvn exec:java -Dexec.mainClass="com.practice.java_10.App"
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< com.practice:java-10 >------------------------
[INFO] Building java-10 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ java-10 ---
[WARNING] 
java.lang.Error: Unresolved compilation problem: 
    var cannot be resolved to a type

    at com.practice.java_10.App.main (App.java:11)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:844)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.019 s
[INFO] Finished at: 2018-03-23T01:51:02+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project java-10: An exception occured while executing the Java class. Unresolved compilation problem: 
[ERROR]     var cannot be resolved to a type
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I am not sure why am I getting this error. Any thoughts are welcome. Meanwhile I try searching for root cause.

Answer

Yserbius picture Yserbius · Mar 22, 2018

You have to set the runtime plugin to Java 10 too or the root configuration. Right now it's just the compiler.