Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
Spring Boot ClassPathResource in a JAR

In my Spring Boot 1.5 application I use ClassPathResource to read a static file the application JAR:

// ...
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

@Slf4j
@Service
public class MyService {
    private Resource resource = new ClassPathResource("a.txt");
    private List<String> myStrings;
    public MyService() {
        myStrings = load(resource);
    }

    private List<String> load(Resource resource) {
        try(Stream<String> stream = Files.lines(Paths.get(resource.getURI()))) {
            myStrings = stream.filter(/* my filter */)
                      .collect(Collectors.toList());
        } catch (IOException x) {
            log.error("Failed to read '{}'.", resource.getFilename());
        }
    }
}

but this fails with:

Caused by: java.nio.file.FileSystemNotFoundException: null
    at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171) ~[zipfs.jar:1.8.0_121]
    at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157) ~[zipfs.jar:1.8.0_121]
    at java.nio.file.Paths.get(Paths.java:143) ~[na:1.8.0_121]
    at MyService.load(MyService.java:53) ~[classes!/:2.0.0-SNAPSHOT]
    //...

How can I read a ClassPathResource embedded in my application JAR?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The JDK's Paths.get cannot resolve resources within JAR files so replace:

    Files.lines(Paths.get(resource.getURI()))

with:

    new BufferedReader(new InputStreamReader(resource.getInputStream())).lines();
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda