web-dev-qa-db-ja.com

varをタイプに解決できません-Java-10

MacマシンにJDK10をインストールしただけです。バージョンを確認しました:-

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)

使用するコンパイラも同じバージョンであることを確認するために、以下を実行しました:-

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

次の構造の単純なMavenプロジェクトを作成しました:-

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

私のpom.xmlは:-

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

私のApp.Javaファイルは次のとおりです。-

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?");

    }
}

私は走ったmvn compileおよびmvn installそして両方ともうまくいきました。以下の証明:-

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

実行されたときmvn exec:Java -Dexec.mainClass="com.practice.Java_10.App"、以下のようなエラーが発生しました:-

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

なぜこのエラーが発生するのかわかりません。どんな考えでも大歓迎です。その間、根本原因を探してみます。

8
Vinay Prajapati

ランタイムプラグインをJava 10にも設定するか、ルート構成に設定する必要があります。現在はコンパイラーのみです。

5
Yserbius