0

I am having a little trouble building jar from gradle project. Namely gradle doesn't include my resource folder even though i have it specified in build.gradle as shown below:

plugins {
    id 'application'
}

repositories {
    mavenCentral()
}

application {
    mainClass = 'skorupinski.xmass.Main'
}

sourceSets {
    main {
        java {
            srcDirs = ['./src/main/java']
        }
        resources {
            srcDirs = ['./src/main/resources']
        }
    }
}

jar {  
    manifest {  
        attributes(  
                'Main-Class': 'skorupinski.xmass.Main'  
        )  
    }  

    baseName = 'hi'
}

There are images in the resource folder that I load into my java program. When I run the jar file it returns an error that it coudln't file the image file unless i put the jar in the same directory as src folder.

My file tree

  • How do you load the image file? I'm quite sure that Gradle does what it always does and includes the resource folder. Maybe your way of trying to load the image is the real problem. – Thomas Kläger Dec 23 '21 at 18:58
  • @ThomasKläger I load it by the path "src/main/resources/image.png", when I run it without building into a jar it works perfectly. – Mateusz Skorupiński Dec 23 '21 at 19:00
  • To get resources placed in my jar file I specify this in my jar task: `from (buildDir) { include 'pattern' }`. – Jeff Holt Dec 23 '21 at 19:02
  • @JeffHolt I tried it this way: ```from('src/main') { include 'resources/*.png' }``` without effect – Mateusz Skorupiński Dec 23 '21 at 19:04
  • What does '.' mean in file/path/directory context?? Correct! Current directory!;) – xerx593 Dec 23 '21 at 19:04
  • To load a packaged resource you must not use the full path ("src/main/resources/image.png") packaging it into the jar drops the first part of the path ("src/main/resource"), within the jar the path is only "image.png" – Thomas Kläger Dec 23 '21 at 19:06
  • @xerx593 after removing '.' before /src/main/resources I can tell you that it wasn't the case – Mateusz Skorupiński Dec 23 '21 at 19:06
  • @ThomasKläger I tried to load it by './image.png' and it still doesnt work. – Mateusz Skorupiński Dec 23 '21 at 19:09
  • 1
    Do you really load the resource (as noted in https://stackoverflow.com/a/15749260: `InputStream is = MyTest.class.getResourceAsStream("/image.png");`) or are you trying to load it from the filesystem (`InputStream is = new FileInputStream("./image.png");`)? Because packaged resource do not exist in the filesystem, you cannot access them with `java.io.File` / `java.io.FileInputStream` and similar – Thomas Kläger Dec 23 '21 at 19:12
  • @ThomasKläger I use this code and as arguments I give "./", "image.png" ```InputStream inputStream = ImageLoader.class.getResourceAsStream(path + filename);``` It returns null – Mateusz Skorupiński Dec 23 '21 at 19:36
  • `/` means root (*when* you are lucky..in windows, it will point to `C:\`).. – xerx593 Dec 23 '21 at 19:40
  • If the image file is in the resources folder directly then the path must be "/" - using "./" will search it in the same directory as the `ImageLoader` class (i.e. if the `ImageLoader` class is in the com.example package using "./image.png" will search the image in the "com/example" resource folder. – Thomas Kläger Dec 23 '21 at 20:17

1 Answers1

0

May be you should try exclusively specifying the include in jar section of build.gradle

jar { 

// try adding this
    from('src/main/resources') {
       include '**/*.*'
    }

 
    manifest {  
        attributes(  
                'Main-Class': 'skorupinski.xmass.Main'  
        )  
    }  

    baseName = 'hi'
}
Sukhmeet Sethi
  • 616
  • 5
  • 14
  • It now includes the directory, but returns an error that every single png in there is a duplicate and there is no duplicate strategy set. – Mateusz Skorupiński Dec 23 '21 at 19:18
  • So the files are already there in the jar. Then the problem could be with your code as to how your are accessing it through the code or may be something wrong with manifest file. Could you pls unpack your jar and check it's manifest file? I am particularly interested to see the `Class-Path` definition – Sukhmeet Sethi Dec 23 '21 at 19:26