0

I am using JetBrains IntelliJ IDEA IDE. This is what I used to generate the jar file. Running the jar file from the IDE, everything looks fine.

Running the jar from the terminal, none of the images are loaded.

My feeling is, from reading around on this, that this has something to do with the relative paths used for the images... but I can't figure this out. I've tried various different project folder structures suggested on the JetBrains forums and StackOverflow, to no avail. Everything is fine until I run a jar outside of the IDE.

My current project structure:

enter image description here

How on Earth do I create a jar file that works everywhere?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Totem
  • 7,189
  • 5
  • 39
  • 66
  • have you set your working directory in project settings? (to `res`?) and is the `res` folder being exported into the jar artifact? you can check by going to Build artifact -> Edit and see if it is included in "Output Layout" – Salem Feb 11 '17 at 20:46
  • @1blustone Not manually no.. where exactly is this done? Everything works fine except when a jar is run outside of the ide – Totem Feb 11 '17 at 20:47
  • assuming you are using the artifact system to build your jar, Build -> Build artifacts -> Edit... -> Output Layout -> the green + button -> Directory Contents, though I am not sure if you would then have to remove the res/ prefix from your paths (i.e. I am not sure if IntelliJ includes the _contents_ or the directory itself.) [It is also possible to set a folder's type to "Resource folder" and I believe it would do this automatically.](http://stackoverflow.com/questions/33314388/how-to-access-images-from-a-resource-folder-in-intellij-idea-without-having-to-g) – Salem Feb 11 '17 at 20:48
  • @1blustone I did this and it didn't seem to change anything unfortunately – Totem Feb 11 '17 at 20:52
  • I'm trying it out now. resource folders seemed to work for me. – Salem Feb 11 '17 at 20:55
  • @1blustone hmm, I can't find a way to mark folders in intellij idea. The link you provided above leads to instructions for webstorm.. but it's not the same on idea. Google search hasn't turned up anything useful so far. Did you mark a folder as a 'resource folder'? If so, how? – Totem Feb 11 '17 at 21:01

3 Answers3

2

Ok, so here's what worked for me. I opened the project structure window (Ctrl-Alt-Shift + S) and went to the Modules tab. From there, I could easily select a folder from the list and click to make it a Resource directory. I was then able to access the resources as URL's with

URL imageUrl = ClassLoader.getSystemResource("image.png")

No need to use a path to the image, just it's actual name.ext

Totem
  • 7,189
  • 5
  • 39
  • 66
1

Using IntelliJ's resource folders is probably the right way to go.

Simply right click your res folder, go to Mark Directory As and select Resources.

enter image description here

Then you can access files in this folder simply by name (without a res/ prefix.)

Working visual example:

enter image description here

Salem
  • 13,516
  • 4
  • 51
  • 70
  • The only folder with the other options available is the 'out' folder – Totem Feb 11 '17 at 21:04
  • ok I thought that might be it, and changed it to resources but still no dice... I'll restart the project and see – Totem Feb 11 '17 at 21:06
  • They are all of the form 'resources/image.png' – Totem Feb 11 '17 at 21:08
  • @Totem hmm. Using resource folders, they shouldn't include the directory name - does that work? (Also, does the icon of your res folder match that of my sample `resources` folder?) – Salem Feb 11 '17 at 21:09
  • The icon doesn't match, I'll put up a shot of it in the question – Totem Feb 11 '17 at 21:10
  • There's a screenshot of the structure above – Totem Feb 11 '17 at 21:13
  • @Totem I am not quite sure what that folder icon is and cannot seem to find it anywhere, but I have had that once and that was the exact type of folder that could not be edited. Try recreating your project, I don't think this should usually happen - sorry to not be of much help. – Salem Feb 11 '17 at 21:23
  • 1
    No worries.. just surprised that there doesn't seem to be any readily available documentation on this. Thanks – Totem Feb 11 '17 at 21:27
1

Source and Resource roots are handled differently.

All the files from the Resource directories will be always copied to the output path. As suggested in another answer, one of the solutions is to configure this directory as Resources root. It's the preferred way to get it working.

If you want files from the Source roots to be copied to the output, you have to specify the patterns for the files that will be copied.

If the project is Maven or Gradle based, these patterns have no effect and IDE will use the rules of the corresponding build system to process the resources.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904