Can I list the resources in a given package?

Lets assume my classes are loaded from a compressed .war file or loaded elsewhere, how can I discover all the resources in a given package? Enumerating files will not really work, since this is a war file. Most likely this will involve using the current classloader?

Is there a library out there that does something like that? Googling revealed only some hacks with listing files.

Asked by: Melissa711 | Posted: 28-01-2022

Answer 1

Yes. You can use ClassLoader.getResources("");

Or you could use ferret API:

This API allows you to scan the class path as well as arbitrary archives and directories using the Visitor pattern.

Answered by: Alina365 | Posted: 01-03-2022

Answer 2

No, you can't list resources in a package because the JVM simply doesn't know what resources are in what package. See this question.

Answered by: Chester171 | Posted: 01-03-2022

Answer 3

The Spring Framework offers a class called PathMatchingResourcePatternResolver. With this class you can give Ant style paths with wildcards and it will find the relevant resources for you (searching through the folders and jars on the classpath).

Answered by: Adelaide439 | Posted: 01-03-2022

Answer 4

I do this: - before packaging, ls myResources/*.ext > myResources/list.txt - then just read the file first

Answered by: Julian438 | Posted: 01-03-2022

Similar questions

java - Why does the Maven goal "package" include the resources in the jar, but the goal "jar:jar" doesn't?

when I package my project with the Maven goal "package", the resources are included as well. They are originally located in the directory "src/main/resources". Because I want to create an executable jar and add the classpath to the manifest, I'm using maven-jar-plugin. I've configured it as the following likes: <plugin> <groupId>org.apache.maven.plugins</groupId> <ar...

java - How to import the classes within a jar file in the folder of resources, to another class within a package?

I have used Intellj to create a project(Spring, actually, but I found this problem to have nothing between Java project and Spring project) whose hierachy lies below: project_name src main java com.x.y controller_package

java - Creating friendly urls to dynamic resources in struts2

I have a struts2 application with a single page that may show one of a number of values stored in a database. The application is for a school with many departments and each department has many programs. The department page is accessed using a url like this department.action?id=2 and the DepartmentAction will load the Department with id = 2 for display. All this is fine if the user is just ...

java - different WAR files, shared resources

Suppose you have several applications which share the same code and most of the other resources, but have a somewhat different look and feel, some labels change, etc. (think branding). If each web app is to go in its own WAR file, where do you put the shared resources? I already use the classpath to share classes and property files. But what about javascript and css files? Is the best way to create and deploy one ...

Do 2D arrays use more resources than 1D arrays in Java?

For example, would a full int[50][8] use more resources (RAM and CPU) than 8 full int[50] arrays?

Best resources for a Java developer working on .NET solutions

Closed. This question does not meet Stack Overflow guid...

How to walk through Java class resources?

I know we can do something like this: Class.class.getResourceAsStream("/com/youcompany/yourapp/module/someresource.conf") to read the files that are packaged within our jar file. I have googled it a lot and I am surely not using the proper terms; what I want to do is to list the available resources, something like this: Class.class.listResources("/com/yourcompany/yo...

java - How to debug JVM resources loading?

To debug class loading in a JVM we can use the param -verbose:class, but... Anyone knows how to debug resources loading (e.g. properties files)?

java - Ant compile doesn't copy the resources

I created my own build.xml which has: <target name="compile"> <mkdir dir="build"/> <javac destdir="build"> <src path="src"/> </javac> </target> <target name="build" depends="compile"> <mkdir dir="dist"/> <jar destfile="dist/app.jar" basedir="build" /> </target> <target name="run" depends="compile"> <jav...

java - Good books or online resources for learning Hibernate

URL to load resources from the classpath in Java

In Java, you can load all kinds of resources using the same API but with different URL protocols: file:///tmp.txt jar:!/COM/foo/Quux.class This nicely decouples the actual loading of the resource from the application that needs the resource, and since a URL is just a String, resource loading is also very easily configurable.

java - Keeping i18n resources synced

I am looking for an editor/comparator for i18n property files that would help me keep different language files in sync. Basically, something that would compare a bunch a of property files and show which keys are not present in a particular language. a property would look something like component.titlepage.title = hello world A simple diff is not possible since the right-han...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)