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 8013525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:39:19+00:00 2026-06-04T19:39:19+00:00

how can I use resources from other maven modules? My goal is to provide

  • 0

how can I use resources from other maven modules? My goal is to provide a AbstractImportClass as well as the to be imported files in a specific maven module. And use this module within other modules extending this class.

Let’s say ModuleA contains src/main/java/MyAbstractImportClass.java, and src/main/resources/MyImport.csv

I now want to use the abstract import class in ModuleB. Or rather, I will extend it, use the abstract-fileimport, and a few custom functions.
Then ModuleC also uses the abstracts’ import and some custom functions.

The problem is: the import in abstract class goes with reader and InputStream. When I execute just ModuleA everything is fine.

But when I tried to include the module via maven pom, and then extend the module to call the import, then I get NullPointerException at the line where the reader is used.

So obvious I cannot use foreign module resources this way.

But how could I instead make use of this?


Update:

Module A:

src/main/java/path/to/MyClassA.java
src/main/resources/path/to/test.txt

abstract class MyClassA {
    public static String TESTFILE = test.txt;

    List<String> doImport(String filename) {
        InputStream fileStream = resourceClass.getResourceAsStream(filename);
        //some precessing
        return list;
    }
}

Module B:

src/main/java/path/to/MyClassB.java

class MyClassB implements MyClassA {
    List<String> list = doImport(TESTFILE);
}

If I put MyClassB in same dir as A, then everything works fine.
If I build B in a own module I get NullPointer for InputStream, what means the file is not found.

  • 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-04T19:39:20+00:00Added an answer on June 4, 2026 at 7:39 pm

    I don’t think your problem is related to Maven at all. Class.getResourceAsStream() resolves relative paths as relative to the class object that you call it on. Therefore, if you use that method in an abstract class, every subclass of it could be looking for the resource in a different place.

    For example, given three classes:

    Super:

    package com.foo;
    public class Super {
        { System.out.println(getClass().getResourceAsStream("test.properties")); }
    }
    

    Sub1, a subclass of Super:

    package com.foo.bar;
    import com.foo.Super;
    public class Sub1 extends Super {}
    

    Sub2, another subclass:

    package com.foo.bar.baz;
    import com.foo.Super;
    public class Sub2 extends Super {}
    

    If you create a Super, it’ll look for the classpath resource “/com/foo/test.properties” because that’s how the path “test.properties” resolves relative to the class com.foo.Super. If you create a Sub1, it’ll look instead in “/com/foo/bar/test.properties”, and for a Sub2 instance, it’ll look in “/com/foo/bar/baz/test.properties”.

    You might want to use an absolute path to the resource instead of a relative one, or else have the subclasses specify paths relative to themselves. It depends on your design and what kind of abstraction you’re trying to achieve.

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

Sidebar

Related Questions

I can use this maven plugin maven-jaxb-plugin to generate Java Classes from XSD file.
R class on android has it's limitations. You can't use the resources dynamically for
I'm looking for resources and book which one can use to get started with
For an Asp.Net MVC 3 application, I will a use resources files to store
I have a multimodule Maven project. One subproject hosts XSL/XML resource files. The other
I can use Firebug and it will show lots of info about files that
I can use the following to get the contents of a folder with no
Users can use their Google, Facebook or Twitter account to login to a site
I can use .button() to create beautiful submit buttons, but the rest of the
I can use FireFox and FireBug, in a pane, I can open a .css

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.