Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

Spring Files.readString() does not work with Maven set to Java 11

Trying to write a simple application that reads a file and prints out the file's content. I read that Java 11 has Java.io.file library so I used it. I created a .txt file locally.

My main class looks like this:

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();
        }
    }
} 

When running

mvn clean install

to compile and build a jar file, I get a compilation error:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

maybe you try this in your pom:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...