Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Loading a static Texture inside a jar file  (Read 393 times)
0 Members and 1 Guest are viewing this topic.
Offline skappler

Senior Newbie





« Posted 2013-01-25 09:21:43 »

Hey guys!
I'm currently trying to export my game into a single jar file. I used JarSplice to create a Fat Jar.
My Game loads the Spritesheet as a Slick2D Texture in a static variable.

At first I tried to load the Texture like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public static Texture loadTexture(String path) {

      try {
         return TextureLoader.getTexture("PNG",new FileInputStream(path));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

     
      return null;
   }


This worked in Eclipse, but not exported as a jar. I googled for a while and changed it to:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public static Texture loadTexture(String path) {

      try {
         return TextureLoader.getTexture("PNG",MyTextureLoader.class.getResourceAsStream(path));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

     
      return null;
   }


Now the Game won't even work in eclipse and I get a "Exception in thread "main" java.lang.ExceptionInInitializerError" when I try to run the game. More precisely when I try to bind the texture.

Could anyone help me?
Offline Vladiedoo
« Reply #1 - Posted 2013-01-25 09:30:26 »

I recently had the same problem and here is how I fixed it.

1. Instead of putting all my resources in a res folder I put images directly in my texture package, sounds in my audio package, so on and so forth.


2. I use davedes's texture class (thank you davedes for the tutorials and all code has the proper copyrights attached).
1  
2  
3  
4  
5  
6  
7  
   private static void initTexture(String fileName) {
      try {
         textures.add(new aufait.textures.Texture(Textures.class.getResource(("/aufait/textures/" + fileName))));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


Just as another example here is how I load my audio resources.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   private void generateSound(String fileName, int index, boolean lastSound) {
      BufferedInputStream bufferedInputStream = new BufferedInputStream(Audio.class.getResourceAsStream("/aufait/audio/" + fileName));
      WaveData waveFile = WaveData.create(bufferedInputStream);
      if (lastSound) {
         try {
            bufferedInputStream.close();
            waveFile.dispose();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      AL10.alBufferData(buffer.get(index), waveFile.format, waveFile.data, waveFile.samplerate);
   }
Offline skappler

Senior Newbie





« Reply #2 - Posted 2013-01-25 09:35:30 »

Thank you VERY much! It worked perfectly. I didn't think this would be solved that quick Cheesy
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.

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (7 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (45 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!