Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  how to get artifacts built with Intellij IDEA to find included res directory?  (Read 336 times)
0 Members and 1 Guest are viewing this topic.
Offline LoneMensch

Junior Newbie





« Posted 2017-11-13 22:41:46 »

Hi,

I asked this question on stackoverflow a while back and got no answer whatsoever. I have a directory called res included in my game which holds my sound and graphics data. I have a completed demo that I'm  trying to build into a standalone jar artifact from IntelliJ IDEA. I included the directory in the artifact build, but when I run the standalone jar, it looks for the resources by prepending the entire root directory of my computer to the resource directory. Does anyone know how to fix this??

Here is some example output. Notice how it looks for the resource starting at root on my machine:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
jeep@VICTORY ~/Dropbox/WGU/Capstone/C436/Demos $ java -jar Varg.jar
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/axe_throw.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/boss_explode.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/drink_potion.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/enemy_explode.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/enemy_hit.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/flame.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/grab_item.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/hero_dead.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/hero_hurt.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/squish.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/switch.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/textbox.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/void_sound.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/water_drain.wav (No such file or directory)
Error getting resource stream!
/home/jeep/Dropbox/WGU/Capstone/C436/Demos/res/audio/sfx/zap.wav (No such file or directory)
Exception in thread "main" java.lang.NullPointerException
        at audio.Sfx.play(Sfx.java:36)
        at Main.main(Main.java:15)
Shutting down TinySound...
TinySound shutdown complete!



Offline h.pernpeintner

JGO Knight


Medals: 64



« Reply #1 - Posted 2017-11-13 23:28:43 »

How are you passing the path into the lib that should load the resources? How do these paths look like? I suggest you need something like described here https://stackoverflow.com/questions/3823746/open-file-try-filesystem-first-then-jars nur i may made wrong assumptions.
Offline LoneMensch

Junior Newbie





« Reply #2 - Posted 2017-11-13 23:33:52 »

Quote
How are you passing the path into the lib that should load the resources?

I pass them like this:

1  
public static final Sfx VOID_SOUND = new Sfx(TinySound.loadSound(new File("res/audio/sfx/void_sound.wav")));


I didn't do a very good job of explaining some things. I just posted a new question on SO with more detail, so I'll link it rather than re type it all here:

https://stackoverflow.com/questions/47275235/how-do-you-properly-include-a-resource-folder-in-a-jar-artifact-in-intellij-idea

I think I articulated it better in that post.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline LoneMensch

Junior Newbie





« Reply #3 - Posted 2017-11-14 00:43:56 »

I'm going to go out on a limb here and say that I think the answer is that it isn't possible. I think the only way resources can be readable from inside the jar file is if they are somewhere in the classpath. In my case I have resources (maps) that write to disk and are read later, that way the state of the map is persistent when you transition from zone to zone. The problem is that you can't write files to the classpath; they must be in an external directory. However, I probably should move the sound effects to the classpath, since those aren't being written.
Offline nsigma
« Reply #4 - Posted 2017-11-14 09:22:02 »

You are passing a relative path to File so it's resolving that against the directory you're running the JAR from to get the absolute paths you posted.  This is what you're telling it to do!  What path are you expecting?

Using relative paths with a JAR is error prone unless you have a launching script (or use the getprotectiondomain().getcodesource().getlocation() workaround) - the path will be wherever you launch the JAR from, not the location of the JAR itself.

If you want to put the resources in the JAR you'll need to switch to getClass().getResource()

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline h.pernpeintner

JGO Knight


Medals: 64



« Reply #5 - Posted 2017-11-14 09:48:10 »

Yes, I assumed that this is the problem, because it always is, when people try to include ressources into their artifact Smiley In the link I posted is an example how you can make a distinction between "run from the ide" and "run from production-single-artifact-build". Basically it's a configuration based switch that switches between file system loading and the getClass().getResource() idea nsigma mentioned already.
Offline LoneMensch

Junior Newbie





« Reply #6 - Posted 2017-11-14 21:31:48 »

Yes, thank you both; I understand. If you want to access resources that are inside the JAR, they must exist inside the classpath and are accessible using a class loader. You can perform a conditional test to determine whether to use the class loader or access using a relative path. If you try to access internal files using a relative path, the path will resolve to the path where the JAR exists, to inside the JAR, so it's only good for external resources. It's clear now.  Smiley
Offline nsigma
« Reply #7 - Posted 2017-11-14 21:58:06 »

the path will resolve to the path where the JAR exists

Yes, but this bit is incorrect.  It will resolve to where you launch the JAR from (ie. if from terminal the pwd, not necessarily where the JAR is).  Add in the possibility of users setting up symlinks, etc. and relying on the relative path can be a bad idea.  See this old message with a bash launcher script that works around this issue (from the Linux / OSX NetBeans launcher) http://www.java-gaming.org/topics/cannot-run-a-program-by-clicking-on-its-icon-whereas-it-works-in-command-line/36811/msg/350522/view.html#msg350522


Or, as mentioned above, in your Java code you can use
1  
URL jarLocation = MyClass.class.getProtectionDomain().getCodeSource().getLocation();

see https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline LoneMensch

Junior Newbie





« Reply #8 - Posted 2017-11-15 14:28:39 »

Thanks, that clears it up nicely.
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (13 views)
2017-11-21 16:21:00

xxMrPHDxx (10 views)
2017-11-21 16:14:31

xxMrPHDxx (10 views)
2017-11-21 16:10:57

Ecumene (113 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (246 views)
2017-08-02 12:28:51

cybrmynd (242 views)
2017-08-02 12:19:43

cybrmynd (242 views)
2017-08-02 12:18:09

Sralse (257 views)
2017-07-25 17:13:48

Archive (875 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!