Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Loading resource with Java in both IDE & from JAR (from static context)

My folder structure inside src is like so:

src
├── main
│   ├── java
│   │   └── com
│   │       └── organization
│   │           └── projectname
│   │               ├── Transformer.java
│   │               └── Utils.java
│   └── resources
│       └── config
└── test
    ├── java
    │   └── com
    │       └── organization
    │           └── projectname
    │               └── AppTest.java
    └── resources
        └── file
            └── test.txt

and I want to read the test.txt file.

I do use Maven & Eclipse, but I'd like the same code to run when I package in JAR (with mvn package) and when someone is running inside IDE or by commandline.

My attempt here fails (gets null):

    InputStream input =  Scorer.class.getResourceAsStream("file/test.txt");
    System.out.println(input);  // prints null

as does this attempt (gets null):

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource("file/test.txt");
    System.out.println(url.getFile());  // prints null

I'm quite confident the file does exist at that location inside src/test/resources/file/, so I'm wondering what else I am doing wrong.

Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Given that test.txt is in src/test/resource it will not be available in the jar file nor when you run the app in your IDE, unless if you're running test units. Here is an Introduction to the Standard Directory Layout

I suggest to move it in src/main/resource.

Moving into src/main/resource will make able your IDE to have the resource available when you run or debug your project.

On the other hand, Maven will be able to copy the resources into the jar when you run the mvn package goal.

To be sure that mvn package goal copy the files into the Jar just add these lines into your pom project:

<build>
  <resources>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
           <include>**.txt</include>
        </includes>
    </resource>
  </resources>
</build>

Have a look at Maven Resource Plugin to understand how to write the <resource> configuration.

And pay attention to this: if you update your maven pom.xml file, adding one or more <resource> part this does not mean that conversely your IDE update the project with this behaviour/configuration (i.e. copy the resources).

So each time you have to check both Maven pom.xml and the IDE project configuration.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda