IntelliJ IDEA artifact.jar and ImagePath

Asked by Jaroslav Novotny on 2020-10-17

Hello, I am having trouble locating pattern images bundled inside .jar in IntelliJ IDEA IDE.

My project is setup like this:

https://i.imgur.com/rTxqZec.png

And the code is:

ImagePath.add("Main/patterns/")
for (ImagePath.PathEntry path : ImagePath.getPaths()) {
    print(String.format("ImagePath: %s", path) + "\n");
}

Which works nicely when I run the project. I get:

null
/C:\(...)\patterns/

And everything works. But when I try to pack it inside .jar and I build an artifact and run it, I just get:

null
null

Weirdly the ImagePath.add("Main/patterns/") returns true. But finding patterns does not work.

This is the .jar structure when I unzip it:

https://i.imgur.com/QY8CCDG.png

Thanks for any help.

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Jaroslav Novotny
Solved:
2020-10-26
Last query:
2020-10-26
Last reply:
2020-10-25
RaiMan (raimund-hocke) said : #1

In the jar there is a folder "main" and your running class is named "Main".

This might lead to a confusion.

I made a new project just to investigate, got some new info. I was hoping it was conflict in the "main" name, but looks like something else.

This is the code:

import org.sikuli.script.*;
public class SikuliTest {
    public static void main(String[] args) {
        boolean path_add_success = ImagePath.add("SikuliTest/patterns/");
        System.out.printf("SikuliTest/patterns/ added?: %s" + "\n", path_add_success);
        for (ImagePath.PathEntry path : ImagePath.getPaths()) {
            System.out.printf("ImagePath: %s\n", path);
        }
        Pattern pattern = new Pattern("pattern.png");
    }
}

When running from IntelliJ all is fine. When from this .jar (unzipped structure: https://i.imgur.com/kILFr1e.png) I get output:

e:\.....\Sikuli_Test_jar>java -jar Sikuli_Test.jar
SikuliTest/patterns/ added?: true
ImagePath: null
ImagePath: null
[error] Image: load: failed: jar:file: /E:/......./out/artifacts/Sikuli_Test_jar/Sikuli_Test.jar!/patterns//pattern.png

Weirdly the "/" gets doubled.

I can provide this test project and .jar if needed.

RaiMan (raimund-hocke) said : #4

Yes, send me the stuff zipped to sikulix---at---outlook---dot---com

RaiMan (raimund-hocke) said : #5

got the stuff - will come back

RaiMan (raimund-hocke) said : #6

Here is my conclusion:

- Do not use a trailing slash:
boolean path_add_success = ImagePath.add("SikuliTest/patterns/");

should be:
boolean path_add_success = ImagePath.add("SikuliTest/patterns");

This does not harm in the "run-inside-IDEA"-situation, but crashes the image load in the jar-situation

(I will evaluate this further and make it more graceful)

- an imagepath "class/folder" is accepted in all cases and works inside IDEA and in the jar situation

... but it seems, that only the first jar found in imagepath is evaluated (this is a bug)

I made my tests with Java 8 and Java 11.

The jar situation does not work with Java 15 (under evaluation, Java 15 is currently not supported anyways)

Yes, all works without the trailing slash! Thanks!