Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Eclipse project file layout  (Read 880 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2013-01-27 17:53:38 »

Hey Smiley I know this isn't exactly Java related, but I'm having an issue with the structure of my project in Eclipse.
The structure goes:

-Project folder
--bin
---com
----test
-----Main.class

--res
---spriteSheet.png

--src
---com
----test
-----Main.java

The issue is that I'm unable to read the spriteSheet.png file inside the res folder from the Main.class file inside the bin folder using the following line inside the Main class:

1  
image = ImageIO.read(getClass().getClassLoader().getResourceAsStream("../../../res/spriteSheet.png"));


I've tried this among other combinations, but it keeps producing this error:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
   at javax.imageio.ImageIO.read(Unknown Source)
   at com.test.Main.<init>(Main.java:15)

It works when I move the res folder inside the bin folder, although I've had issues doing it that way in the past where the folder has been wiped.
Is this a poor project structure and if so, could somebody explain to me what a good structure would be and also why this is producing an error?

Thanks,

Paul
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-01-27 18:06:33 »

You're going through the classloader (as you should) to get the resource, so your path is already absolute -- those extra "../" segments are doing nothing but adding confusion.  Next, make your res folder a source folder, and it will be copied into bin on build as a new root (don't use "res/" in the path).  Once you've done that and told eclipse to build, you should be able to do this:

getClass().getClassLoader().getResourceAsStream("spriteSheet.png");


Last, Don't ever screw with the bin/ folder directly.  Only eclipse should ever touch that -- you should be able to delete the entire bin/ folder at any time and let eclipse rebuild it.
Offline Porlus

Junior Member





« Reply #2 - Posted 2013-01-27 18:18:26 »

Thanks for your reply. Smiley I've just moved the res folder to src, but I foolishly deleted the bin folder thinking it would rebuild it entirely and now when I attempt to run it, it gives me this error:

Error: Could not find or load main class com.test.Main

Also I've got it on automatic build and I think I've buggered up the project.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Porlus

Junior Member





« Reply #3 - Posted 2013-01-27 18:30:06 »

Ah, I just renamed the file and it's rebuilt. Smiley Thanks for your help mate.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2013-01-27 18:33:55 »

You don't need to put the res folder under src, you can mark it as a source folder in the project settings.  That's actually what I meant by making it a source folder.

Also, after rearranging folders, you should do a clean and full rebuild.

Online princec

JGO Kernel


Medals: 386
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2013-01-28 09:25:41 »

@sproingie - we've found that
1  
Thread.currentThread().getContextClassLoader().getResource(...)

is the only truly reliable way of getting resources. The classloader for the class you're in can sometimes not have access to resources for odd reasons.

Cas Smiley

Online princec

JGO Kernel


Medals: 386
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #6 - Posted 2013-01-28 09:27:10 »

...also we've found that making /res a source folder means that Eclipse in its wisdom copies the contents of /res into /bin, which can lead to very strange bugs when for whatever reason the contents of /bin are not quite up to date. So what we do nowadays is we add /res in the launch classpath instead.

Cas Smiley

Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2013-01-28 16:59:49 »

I would really be interested in seeing code that replicated the issue of a user-defined class not having access to user-defined resources if no untoward monkeying around with the thread's CCL has taken place. Maybe some applet environments do exactly such tomfoolery, but I'd still like to see it.

As for copying into /bin, that's usually not a problem, and it means whatever jar you pack will guaranteed have those resources in them with no extra step needed.  I guess it's different for games with large assets or lots of them.
Online princec

JGO Kernel


Medals: 386
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #8 - Posted 2013-01-28 18:52:02 »

Webstart failed in those circumstances pretty readily.

Cas Smiley

Offline deathpat
« Reply #9 - Posted 2013-01-28 19:02:17 »

...also we've found that making /res a source folder means that Eclipse in its wisdom copies the contents of /res into /bin, which can lead to very strange bugs when for whatever reason the contents of /bin are not quite up to date. So what we do nowadays is we add /res in the launch classpath instead.

Cas Smiley

Thanks for the trick, it's always bothering me to refresh my project each time I touch a resource file Wink

work in progress : D A E D A L U S
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!