Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:59:33+00:00 2026-06-16T01:59:33+00:00

In my Maven project, I have a xls file in src/main/resources. When I read

  • 0

In my Maven project, I have a xls file in src/main/resources.
When I read it like this:

 InputStream in = new
 FileInputStream("src/main/resources/WBU_template.xls");

everything is ok.

However I want to read it as InputStream with getResourceAsStream. When I do this, with or without the slash I always get a NPE.

     private static final String TEMPLATEFILE = "/WBU_template.xls";
     InputStream in = this.getClass.getResourceAsStream(TEMPLATEFILE);

No matter if the slash is there or not, or if I make use of the getClassLoader() method, I still get a NullPointer.

I also have tried this :

URL u = this.getClass().getResource(TEMPLATEFILE);
System.out.println(u.getPath());

the console says…/target/classes/WBU_template.xls
and then get my NullPointer.

What am I doing wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:59:34+00:00Added an answer on June 16, 2026 at 1:59 am

    FileInputStream will load a the file path you pass to the constructor as relative from the working directory of the Java process.

    getResourceAsStream() will load a file path relative from your application’s classpath.

    When you use .getClass().getResource(fileName) it considers the location of the fileName is the same location of the of the calling class.

    When you use .getClass().getClassLoader().getResource(fileName)
    it considers the location of the fileName is the root – in other words bin folder.

    The file should be located in src/main/resources when loading using Class loader

    In short, you have to use .getClass().getClassLoader().getResource(fileName) to load the file in your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Maven project structured like this: src/main/java/test/App.java src/main/resources/config/message.txt Inside App.java, I have
I have a multiple maven project like this: root/ ----war ----jar1 ----jar2 ----jar3 The
I have multimodule maven project like this myproject framework1 f1-presentation *.java f1-core *.java f1-tag
I have a multimodule maven project like : Main Module Module A Module B
I have a maven project with the following packages (for illustration only): Root: src/main/java
I have a maven project and I'd like to create a distribution of it
I have a maven project that loads an xslt file and executes the transformation
I have a maven project, and then, i would like to enable JSF 2
I have a maven project in which I would like to unpack all the
I have a Maven project that generates a WAR file and tries to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.