【问题标题】:Spring Files.readString() does not work with Maven set to Java 11Spring Files.readString() 不适用于设置为 Java 11 的 Maven
【发布时间】:2021-01-12 14:42:34
【问题描述】:

尝试编写一个简单的应用程序来读取文件并打印出文件的内容。我读到 Java 11 有 Java.io.file 库,所以我使用了它。我在本地创建了一个 .txt 文件。

我的主课是这样的:

package com.iz.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import javax.swing.text.AbstractDocument.Content;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);        
    
        Path filePath = Paths.get("/Users/iz/Proj/backend/src/testFile.txt");

        try
        {
            String content = Files.readString(filePath);

            System.out.println(content);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
} 

运行时

mvn clean install

编译和构建一个jar文件,我得到一个编译错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project backend: Compilation failure
[ERROR] /home/proj/backend/src/main/java/com/iz/backend/BackendApplication.java:[26,34] error: cannot find symbol
[ERROR] 
[ERROR] -> [Help 1]

【问题讨论】:

  • 您的 maven 是否配置为使用正确的 Java 版本?您可以通过mvn --version 查询。另外,您的 readString 看起来不是在第 26 行,还是我错了?
  • 我使用的是 Apache Maven 3.6.3,在 pom 文件中我定义了 Java 11
  • 您从哪里执行全新安装?
  • 您的 pom.xml 指定 java 11 的事实并不意味着它将使用 java 11。如果您尚未安装 java 11 或您的 java home 指向旧版本,maven 将使用它.
  • javac -version 会为您返回什么?

标签: java spring-boot maven file bufferedreader


【解决方案1】:

也许你可以在你的 pom 中试试这个:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>11</release>
    </configuration>
</plugin>

【讨论】:

    【解决方案2】:

    为我工作,使用以下 pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>11</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

    • 由于某种原因,我无法添加 spring-boot-starter 依赖项。它无法读取它。
    • yes- dependencies.dependency.version' for org.springframework.cloud:spring-cloud-starter:jar is missing
    猜你喜欢
    • 2019-11-24
    • 2021-02-14
    • 2019-02-24
    • 2014-10-01
    • 2018-02-03
    • 2020-02-29
    • 2020-02-04
    • 1970-01-01
    • 2019-08-07
    相关资源
    最近更新 更多