How to get resource files from runnable jar file that are in build path

I just need the files from the jar file that are in the resource directory at runtime ...

I know several ways how ... but it only reads images

ImageIO.read(Login.class.getResource("res/images/icon.png"));

      

Here I can read "Images" from a directory in a JAR that is added to the build path in eclipse, but here I am running into some problem getting some ".so" file and some "cascade_hand.xml" file from resources / cascades / .. ., I need them to be available at runtime from the .JAR executable.

I tried this too ... but it doesn't work.

CascadeClassifier cascade = new CascadeClassifier(
            "resources/cascades/haarcascade_mcs_leftear.xml");

      

And it doesn't work ...

CascadeClassifier cascade = new CascadeClassifier(this.getClass().getResourceAsStream("resources/cascades/haarcascade_mcs_leftear.xml");

      

I know there is a way to copy this file to some temporary directory and use them, but I really don't know how?

InputStream is = this.getClass().getResourceAsStream("resources/cascades/haarcascade_mcs_leftear.xml");

      

Is there any other way to access these types of files from the Runnable JAR?

I also had a problem with the jar file generated with eclipse.

Eclipse creates a Runnable jar file and creates resource directories in the jar file that are in the build path, but in reality they are all created as a single parent directory in the jar file.

I have a folder hierarchy in eclipse similar to the following image.

enter image description here

+3


source to share


2 answers


Unfortunately, OpenCV reads the file natively, so the only way is to copy the data to a temporary file first.



String tempDir="tmp"; //TODO: use a sensible default
Path tmp = Files.createTempFile(tempDir, "cv");
Files.copy(Login.class.getResourceAsStream(
    "resources/cascades/haarcascade_mcs_leftear.xml"), tmp);
CascadeClassifier cascade = new CascadeClassifier(tmp.toString());

      

+2


source


You can use a classloader to read the file

InputStream inn = ClassLoader
            .getSystemResourceAsStream("resources/cascades/haarcascade_mcs_leftear.xml");

      



Hope it helps.

0


source







All Articles