Loading resource from within project












1















I have a main project which has dependencies on multiple other projects.
Each project can have a folder called "images" under src/main/resources.
How could I make sure that every project loads its own images, not the ones from other projects?



What I've tried is this:



URL dirURL = this.getClass().getResource("/images/");
(new File(dirURL.toURI())).list()


The problem is that the program loads the first directory called "images" found in the classpath. If I have the projects "loaderA" and "loaderB", no matter where would the code above reside, it would always be read from project "loaderA".



How could I avoid this?










share|improve this question























  • Resources are not files. You cannot treat them like directories. Some workarounds exist, but they are not reliable. Your best option is to include a plain text resource that lists all the available images in your application.

    – VGR
    Nov 19 '18 at 15:39











  • @VGR That would work as well. How can I make sure that I don't have the same issues with that file as well(being read from the wrong project)?

    – Dragos
    Nov 19 '18 at 15:44











  • Resources belong in packages. This is why, if you pass a string to getResource which does not start with a slash, the default behavior of getResource is to look in the same package as the Class object.

    – VGR
    Nov 19 '18 at 15:46











  • @VGR And is there a way to ensure package names won't collide and get me in the same situation? As you probably imagine, I'm not in full control of all the projects/packages here. Sorry if it sounds like a dumb question.

    – Dragos
    Nov 19 '18 at 15:56











  • If the projects aren’t under your control, you can set the classpath so the desired library comes first. Another option is reading from all the projects, using getClass().getClassLoader().getResources(resourcePath), but that won’t give you the ability to reliably discern which project each resource came from. (You could examine each URL directly, but do you really want to rely on that?)

    – VGR
    Nov 19 '18 at 16:30
















1















I have a main project which has dependencies on multiple other projects.
Each project can have a folder called "images" under src/main/resources.
How could I make sure that every project loads its own images, not the ones from other projects?



What I've tried is this:



URL dirURL = this.getClass().getResource("/images/");
(new File(dirURL.toURI())).list()


The problem is that the program loads the first directory called "images" found in the classpath. If I have the projects "loaderA" and "loaderB", no matter where would the code above reside, it would always be read from project "loaderA".



How could I avoid this?










share|improve this question























  • Resources are not files. You cannot treat them like directories. Some workarounds exist, but they are not reliable. Your best option is to include a plain text resource that lists all the available images in your application.

    – VGR
    Nov 19 '18 at 15:39











  • @VGR That would work as well. How can I make sure that I don't have the same issues with that file as well(being read from the wrong project)?

    – Dragos
    Nov 19 '18 at 15:44











  • Resources belong in packages. This is why, if you pass a string to getResource which does not start with a slash, the default behavior of getResource is to look in the same package as the Class object.

    – VGR
    Nov 19 '18 at 15:46











  • @VGR And is there a way to ensure package names won't collide and get me in the same situation? As you probably imagine, I'm not in full control of all the projects/packages here. Sorry if it sounds like a dumb question.

    – Dragos
    Nov 19 '18 at 15:56











  • If the projects aren’t under your control, you can set the classpath so the desired library comes first. Another option is reading from all the projects, using getClass().getClassLoader().getResources(resourcePath), but that won’t give you the ability to reliably discern which project each resource came from. (You could examine each URL directly, but do you really want to rely on that?)

    – VGR
    Nov 19 '18 at 16:30














1












1








1








I have a main project which has dependencies on multiple other projects.
Each project can have a folder called "images" under src/main/resources.
How could I make sure that every project loads its own images, not the ones from other projects?



What I've tried is this:



URL dirURL = this.getClass().getResource("/images/");
(new File(dirURL.toURI())).list()


The problem is that the program loads the first directory called "images" found in the classpath. If I have the projects "loaderA" and "loaderB", no matter where would the code above reside, it would always be read from project "loaderA".



How could I avoid this?










share|improve this question














I have a main project which has dependencies on multiple other projects.
Each project can have a folder called "images" under src/main/resources.
How could I make sure that every project loads its own images, not the ones from other projects?



What I've tried is this:



URL dirURL = this.getClass().getResource("/images/");
(new File(dirURL.toURI())).list()


The problem is that the program loads the first directory called "images" found in the classpath. If I have the projects "loaderA" and "loaderB", no matter where would the code above reside, it would always be read from project "loaderA".



How could I avoid this?







java maven file dependencies






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 15:33









DragosDragos

1,48683149




1,48683149













  • Resources are not files. You cannot treat them like directories. Some workarounds exist, but they are not reliable. Your best option is to include a plain text resource that lists all the available images in your application.

    – VGR
    Nov 19 '18 at 15:39











  • @VGR That would work as well. How can I make sure that I don't have the same issues with that file as well(being read from the wrong project)?

    – Dragos
    Nov 19 '18 at 15:44











  • Resources belong in packages. This is why, if you pass a string to getResource which does not start with a slash, the default behavior of getResource is to look in the same package as the Class object.

    – VGR
    Nov 19 '18 at 15:46











  • @VGR And is there a way to ensure package names won't collide and get me in the same situation? As you probably imagine, I'm not in full control of all the projects/packages here. Sorry if it sounds like a dumb question.

    – Dragos
    Nov 19 '18 at 15:56











  • If the projects aren’t under your control, you can set the classpath so the desired library comes first. Another option is reading from all the projects, using getClass().getClassLoader().getResources(resourcePath), but that won’t give you the ability to reliably discern which project each resource came from. (You could examine each URL directly, but do you really want to rely on that?)

    – VGR
    Nov 19 '18 at 16:30



















  • Resources are not files. You cannot treat them like directories. Some workarounds exist, but they are not reliable. Your best option is to include a plain text resource that lists all the available images in your application.

    – VGR
    Nov 19 '18 at 15:39











  • @VGR That would work as well. How can I make sure that I don't have the same issues with that file as well(being read from the wrong project)?

    – Dragos
    Nov 19 '18 at 15:44











  • Resources belong in packages. This is why, if you pass a string to getResource which does not start with a slash, the default behavior of getResource is to look in the same package as the Class object.

    – VGR
    Nov 19 '18 at 15:46











  • @VGR And is there a way to ensure package names won't collide and get me in the same situation? As you probably imagine, I'm not in full control of all the projects/packages here. Sorry if it sounds like a dumb question.

    – Dragos
    Nov 19 '18 at 15:56











  • If the projects aren’t under your control, you can set the classpath so the desired library comes first. Another option is reading from all the projects, using getClass().getClassLoader().getResources(resourcePath), but that won’t give you the ability to reliably discern which project each resource came from. (You could examine each URL directly, but do you really want to rely on that?)

    – VGR
    Nov 19 '18 at 16:30

















Resources are not files. You cannot treat them like directories. Some workarounds exist, but they are not reliable. Your best option is to include a plain text resource that lists all the available images in your application.

– VGR
Nov 19 '18 at 15:39





Resources are not files. You cannot treat them like directories. Some workarounds exist, but they are not reliable. Your best option is to include a plain text resource that lists all the available images in your application.

– VGR
Nov 19 '18 at 15:39













@VGR That would work as well. How can I make sure that I don't have the same issues with that file as well(being read from the wrong project)?

– Dragos
Nov 19 '18 at 15:44





@VGR That would work as well. How can I make sure that I don't have the same issues with that file as well(being read from the wrong project)?

– Dragos
Nov 19 '18 at 15:44













Resources belong in packages. This is why, if you pass a string to getResource which does not start with a slash, the default behavior of getResource is to look in the same package as the Class object.

– VGR
Nov 19 '18 at 15:46





Resources belong in packages. This is why, if you pass a string to getResource which does not start with a slash, the default behavior of getResource is to look in the same package as the Class object.

– VGR
Nov 19 '18 at 15:46













@VGR And is there a way to ensure package names won't collide and get me in the same situation? As you probably imagine, I'm not in full control of all the projects/packages here. Sorry if it sounds like a dumb question.

– Dragos
Nov 19 '18 at 15:56





@VGR And is there a way to ensure package names won't collide and get me in the same situation? As you probably imagine, I'm not in full control of all the projects/packages here. Sorry if it sounds like a dumb question.

– Dragos
Nov 19 '18 at 15:56













If the projects aren’t under your control, you can set the classpath so the desired library comes first. Another option is reading from all the projects, using getClass().getClassLoader().getResources(resourcePath), but that won’t give you the ability to reliably discern which project each resource came from. (You could examine each URL directly, but do you really want to rely on that?)

– VGR
Nov 19 '18 at 16:30





If the projects aren’t under your control, you can set the classpath so the desired library comes first. Another option is reading from all the projects, using getClass().getClassLoader().getResources(resourcePath), but that won’t give you the ability to reliably discern which project each resource came from. (You could examine each URL directly, but do you really want to rely on that?)

– VGR
Nov 19 '18 at 16:30












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377923%2floading-resource-from-within-project%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377923%2floading-resource-from-within-project%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?