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  
  LWJGL game works fine in eclipse, but doesn't when exported  (Read 700 times)
0 Members and 1 Guest are viewing this topic.
Offline Ablaze

Junior Newbie





« Posted 2013-12-08 08:55:23 »

Hi!
    I made a small puzzle with textures. I used lwjgl.jar, lwjgl_util.jar and slick-util.jar. It works fine in eclipse but not when exported.

Main.java
1  
2  
3  
4  
5  
6  
7  
package example;

public class Main {
   public static void main(String[] args) {
      new LWJGLInput();
   }
}


LWJGLInput.java
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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
package example;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


public class LWJGLInput {
   
   List<Box> boxes = new ArrayList<Box>(16);
   boolean select = false;
   
   
   public Texture loadTexture(String key){
      try {
         return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }
   public LWJGLInput(){
      try {
         Display.setDisplayMode(new DisplayMode(640, 480));
         Display.setTitle("Puzzle");
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      //65
     boxes.add(new Box(15, 15));
      boxes.add(new Box(200, 150));
      boxes.add(new Box(15, 115));
     
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, 640, 480, 0, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_2D);
     
     
      while (!Display.isCloseRequested()){
         glClear(GL_COLOR_BUFFER_BIT);
         
         
         
         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            Display.destroy();
            System.exit(0);
         }
         
         for(Box box : boxes){
            if(Mouse.isButtonDown(0) && box.isInBounds(Mouse.getX(), 480 - Mouse.getY()) && !select && (boxes.indexOf(box)==1)){
               box.selected = true;
               select = true;
            }
           
            if(Mouse.isButtonDown(1)){
               box.selected = false;
               select = false;
            }
           
            if (box.selected){
               box.update(Mouse.getDX(), -Mouse.getDY());
            }
           
            if(boxes.indexOf(box) == 1 && (box.x == 15) && (box.y == 65)){

               
               if(!box.selected){
                  while(true){
                  System.out.println("You win the game!");
                  System.out.println("Thanks for playing! :)");
                  System.exit(0);
                 
               }
               }
            }
           
            box.draw();
           
         }
     
         
         
         Display.update();
         Display.sync(60);
      }
     
      Display.destroy();
     
     
      }
   

   
}
   


Box.java
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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
package example;

import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glVertex2f;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Box {
      Texture wood;
      int x,y;
      float cRed, cBlue, cGreen;
      public boolean selected = false;
     
      Box(int x, int y){
         this.x = x;
         this.y = y;
         
         Random ran = new Random();
         
         cRed = ran.nextFloat();
         cBlue = ran.nextFloat();
         cGreen = ran.nextFloat();
      }
     
      void randomize(){
         Random ran = new Random();
         cRed = ran.nextFloat();
         cBlue = ran.nextFloat();
         cGreen = ran.nextFloat();
      }
     
      void draw(){
         wood = loadTexture("wood");
         wood.bind();
         
         
         int a = x;
         int b = y;
         int c = x+50;
         int d = y + 50;
         
         glBegin(GL_QUADS);
            glTexCoord2f(0,0);
            glVertex2f(a, b);
            glTexCoord2f(1,0);
            glVertex2f(c, b);
            glTexCoord2f(1,1);
            glVertex2f(c, d);
            glTexCoord2f(0,1);
            glVertex2f(a, d);
         glEnd();
         
      }
     
      void update(int dx, int dy){
         x += dx;
         y += dy;
      }
     
      boolean isInBounds(int mouseX, int mouseY) {
            return mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50;
        }
     
      public Texture loadTexture(String key){
         try {
            return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
         } catch (FileNotFoundException e) {
           
         } catch (IOException e) {
           
         }
         return null;
      }
   

}


I used jarsplice to compile it all. In case you want to inspect the jar file through winrar, here it is:-
https://www.mediafire.com/?oba961abs7sysw3

So here is the problem. I try to run it (double click it), it comes for a second and disappears. This doesn't happen in eclipse.

I tried running it through the command prompt and I got an exception - NullPointer.

1  
2  
3  
4  
Exception in thread "main" java.lang.NullPointerException
        at example.Box.draw(Box.java:44)
        at example.LWJGLInput.<init>(LWJGLInput.java:104)
        at example.Main.main(Main.java:5)


If it isn't null in eclipse, why does the variable become null when exported?

Please help me.

Regards,
Ablaze.
Offline SHC
« Reply #1 - Posted 2013-12-08 09:32:10 »

Never swallow exceptions.

1  
2  
3  
4  
5  
6  
7  
8  
public Texture loadTexture(String key){
    try {
        return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
    } catch (Exception e) {
+       e.printStackTrace();
    }
    return null;
}

Offline Ablaze

Junior Newbie





« Reply #2 - Posted 2013-12-08 09:45:23 »

Never swallow exceptions.

1  
2  
3  
4  
5  
6  
7  
8  
public Texture loadTexture(String key){
    try {
        return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
    } catch (Exception e) {
+       e.printStackTrace();
    }
    return null;
}


Okay, so here is the new stack trace:-
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
java.io.FileNotFoundException: res\wood.png (The system cannot find the path spe
cified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at example.Box.loadTexture(Box.java:76)
        at example.Box.draw(Box.java:43)
        at example.LWJGLInput.<init>(LWJGLInput.java:104)
        at example.Main.main(Main.java:5)
Exception in thread "main" java.lang.NullPointerException
        at example.Box.draw(Box.java:44)
        at example.LWJGLInput.<init>(LWJGLInput.java:104)
        at example.Main.main(Main.java:5)


So the file isn't found. Where exactly am I supposed to place my res folder?

res stands for resources.

Regards,
Ablaze.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rv3392

Senior Member


Medals: 2
Projects: 1
Exp: 5 years


Programmer | Modder | Gamer


« Reply #3 - Posted 2013-12-08 09:48:22 »

I have the same problem that's why I didn't publish Craftia.
Offline SHC
« Reply #4 - Posted 2013-12-08 10:04:47 »

Try changing this line

1  
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));

To

1  
return TextureLoader.getTexture("PNG", this.getClass().getClassLoader().getResourceAsStream("res/" + key + ".png"));

Offline aldacron

Senior Member


Medals: 9
Exp: 16 years


Java games rock!


« Reply #5 - Posted 2013-12-08 10:07:07 »

Bundle your resources in a jar file, put that on the class path along with the other jars you use, then use the resource API to get at them rather than loading directly from the file system. They don't actually have to be in a jar, but it simplifies distribution. Search these forums for getResourceAsStream and you should find a number of posts about it. Or look to Google, where you can also find advice on how to set things up in Eclipse so that this approach works the same way in the IDE and out.

With your current approach, you're trying to load relative to the current working directory. In the IDE, that's easy. Outside, it can be literally anything. Going through the resourceAPI can mask all that.
Offline Ablaze

Junior Newbie





« Reply #6 - Posted 2013-12-08 10:21:19 »

Bundle your resources in a jar file, put that on the class path along with the other jars you use, then use the resource API to get at them rather than loading directly from the file system. They don't actually have to be in a jar, but it simplifies distribution. Search these forums for getResourceAsStream and you should find a number of posts about it. Or look to Google, where you can also find advice on how to set things up in Eclipse so that this approach works the same way in the IDE and out.

With your current approach, you're trying to load relative to the current working directory. In the IDE, that's easy. Outside, it can be literally anything. Going through the resourceAPI can mask all that.

Hmm.. I didn't exactly understand what you said. I just started LWJGL programming day before yesterday, so I don't know much. What is resourceAPI? Can you link me to a tutorial about it? And by class path, what do you mean? I know the system variable PATH, but I don't know what class path is.

Try changing this line

1  
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));

To

1  
return TextureLoader.getTexture("PNG", this.getClass().getClassLoader().getResourceAsStream("res/" + key + ".png"));


Did this, and now it doesn't work even in eclipse.

Regards,
Ablaze.
Offline SHC
« Reply #7 - Posted 2013-12-08 10:27:10 »

Can you post a screenshot of your eclipse package explorer?

Offline aldacron

Senior Member


Medals: 9
Exp: 16 years


Java games rock!


« Reply #8 - Posted 2013-12-08 10:48:41 »

Hmm.. I didn't exactly understand what you said. I just started LWJGL programming day before yesterday, so I don't know much.

This has nothing to do with LWJGL. It's about loading resources in Java.

Quote
What is resourceAPI?

I'm talking about the getResourceAsStream method and it's companion getResource.

Quote
Can you link me to a tutorial about it?

Ask Google. This is basic Java stuff here, so there should be a plethora of information about it out there. I don't know what's good and what's not, though, these days.

Quote
And by class path, what do you mean?

This is where the JVM looks to find the Java classes needed to run your program. Again, basic Java knowledge that a good java tutorial or book can tell you about. Ask Google.

Quote
Did this, and now it doesn't work even in eclipse.

You need to configure things properly for it to work in eclipse.

I strongly suggest you go out and read up on class paths and how to load Java resources, else all the advice you get here is just going to keep leading to issues you don't know how to solve. A couple of good tutorials should help you figure it all out.
Offline Ablaze

Junior Newbie





« Reply #9 - Posted 2013-12-08 10:49:53 »

Hmm.. I didn't exactly understand what you said. I just started LWJGL programming day before yesterday, so I don't know much.

This has nothing to do with LWJGL. It's about loading resources in Java.

Quote
What is resourceAPI?

I'm talking about the getResourceAsStream method and it's companion getResource.

Quote
Can you link me to a tutorial about it?

Ask Google. This is basic Java stuff here, so there should be a plethora of information about it out there. I don't know what's good and what's not, though, these days.

Quote
And by class path, what do you mean?

This is where the JVM looks to find the Java classes needed to run your program. Again, basic Java knowledge that a good java tutorial or book can tell you about. Ask Google.

Quote
Did this, and now it doesn't work even in eclipse.

You need to configure things properly for it to work in eclipse.

I strongly suggest you go out and read up on class paths and how to load Java resources, else all the advice you get here is just going to keep leading to issues you don't know how to solve. A couple of good tutorials should help you figure it all out.

Okay I'll look at it.

Can you post a screenshot of your eclipse package explorer?

Here:-



I've blackened out some personal info.

Regards,
Ablaze.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #10 - Posted 2013-12-08 11:02:54 »

Just drag the 'res' folder into 'src' and it should work.

Offline Ablaze

Junior Newbie





« Reply #11 - Posted 2013-12-08 11:23:31 »

Just drag the 'res' folder into 'src' and it should work.

Thanks, it works!

Can you explain why it wasn't working with the other one?

P.S. I also have another problem, please have a look at it here - http://www.java-gaming.org/topics/after-using-slick-for-textures-game-lags-too-much/31438/view.html.

Regards,
Ablaze.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2013-12-08 11:38:05 »

It wasn't working because you can't use File inside JAR files. You have to use getResourceAsStream(String) from a Class object, which you can obtain by calling getClass().getResourceAsStream(String) on any object or by doing MyClassName.class.getResourceAsStream(String) where "MyClassName" is the root class for relative paths.

In your code, you did new File("res/" + key + ".png"), which without a leading forward slash is a relative path, relative to the current directory: the project root folder. With your "res" folder there, it worked fine. When you used SHC's code for this.getClass().getClassLoader().getResourceAsStream(String), it was looking relative to the package root, which is the "src" folder. Moving the "res" into "src" fixed that.

Recap:
RelativeAbsolute (relative to root)
File (resource NOT in JAR)new File("relative/path/")new File("/relative/to/file/system/root/")
getResource(String)
(Resource in JAR or file system)
getClass().
getResource("relative/to/Class")
getClass().getClassLoader().
getResource("relative/to/package/root")
getResourceAsStream(string)
(Resource in JAR or file system)
getClass().
getResourceAsStream("relative/to/Class")
getClass().getClassLoader().
getResourceAsStream("relative/to/package/root")

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #13 - Posted 2013-12-08 14:29:27 »

As you are using slick-util, another easier option available to you is to use its ResourceLoader and let it worry about whether the resource is in a jar or on the filesystem.

Simply use ResourceLoader.getResourceAsStream(), if you want an InputStream or ResourceLoader.getResource() if you want an URL.

Javadoc here.
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.

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

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

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

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

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

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

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

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
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!