Code Monkey home page Code Monkey logo

Comments (3)

 avatar commented on June 8, 2024

This sample code (taken from https://www.logicbig.com/how-to/java/list-all-files-in-resouce-folder.html) works, but in native-image mode it uses the filesystem even though the URL starts with 'resource:'.

public class Test{

  private static File[] getResourceFolderFiles (String folder) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource(folder);
    String path = url.getPath();
    return new File(path).listFiles();
  }

  public static void main (String[] args) {
    for (File f : getResourceFolderFiles("aFolderInClassPath")) {
    System.out.println(f);
   }
  }
}

Other suggestions or alternatives are welcome :-)

FWIW: I want the files packaged with the native-image to not have an external dependency. Considered using a container-image, but that defeats the purpose a bit and requires a container runtime.

from graal.

 avatar commented on June 8, 2024

This is the Ant task that works for me at the moment:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>${maven-antrun-plugin.version}</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <target>
                    <macrodef name="replaceproperty" taskname="@{taskname}">
                        <attribute name="src" />
                        <attribute name="dest" default="" />
                        <attribute name="replace" default="" />
                        <attribute name="with" default="" />
                        <sequential>
                            <loadresource property="@{dest}">
                                <propertyresource name="@{src}" />
                                <filterchain>
                                    <tokenfilter>
                                        <filetokenizer/>
                                        <replacestring from="@{replace}" to="@{with}"/>
                                    </tokenfilter>
                                </filterchain>
                            </loadresource>
                        </sequential>
                    </macrodef>
                    <delete file="src/main/resources/common/resource-allocation-table" />
                    <defaultexcludes remove="**/.gitignore" />
                    <pathconvert property="resource.files" pathsep="${line.separator}">
                        <fileset dir="src/main/resources" />
                    </pathconvert>
                    <replaceproperty src="resource.files" dest="stripped.resource.files" replace="${user.dir}/src/main/resources/" with=""/>
                    <echo file="src/main/resources/common/resource-allocation-table" message="${stripped.resource.files}" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

from graal.

chirontt avatar chirontt commented on June 8, 2024

@OnnoTIP have a look at this project: bclozel/native-resources

The code can list all resources in the native image starting from root (/).

from graal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.