Problem with getting LWJGL to work outside of Eclipse

I have the problem that when I work in Eclipse I can get a window to open as it should but for some reason I can’t get it to start once I’ve exported the program to a JAR-file. I made a very simple example where I only draw a single image on the center of the screen and tried that. It worked in Eclipse and it didn’t work once exported (in a way I’m glad that is so, it shows the problem is quite basic and not a huge error in my other program).
I’ve added in the code here so that you can see. It works in Eclipse, but not when exported.


import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.ARBDebugOutput.*;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ARBDebugOutputCallback;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.PixelFormat;


public class OpenGL_Example {

	public static void main(String[] args) {
		try {
			//Display.setDisplayMode(new DisplayMode(600, 400));
			Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
			Display.create(new PixelFormat(), new ContextAttribs().withDebug(true)); 
			//Display.setVSyncEnabled(true);
			if(GLContext.getCapabilities().GL_ARB_debug_output){
				glDebugMessageCallbackARB(new ARBDebugOutputCallback());
			}
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
		
		BufferedImage image = null;
		
		try {
			image = ImageIO.read(new File("BucketKitties.jpg"));	//Just your average cat image
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		int width = image.getWidth(), height = image.getHeight();
		int[] pixels = image.getRGB(0, 0, width, height, new int[width*height], 0, width);
		
		ByteBuffer data = BufferUtils.createByteBuffer(3*width*height);
		//System.out.println(data);
		for(int i = 0; i < pixels.length; i++){
			int pixel = pixels[i];
			data.put((byte) ((pixel >> 16) & 0xFF));     // Red component
            data.put((byte) ((pixel >> 8) & 0xFF));      // Green component
            data.put((byte) (pixel & 0xFF));			 // Blue component
		}
		//System.out.println(data);
		data.flip();		//Sets the Reading Marker to the start of the buffer
		//System.out.println(data);
		
		int texture = glGenTextures();
		glBindTexture(GL_TEXTURE_2D, texture);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		
		//We don't need different sized images for this
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		
		glEnable(GL_TEXTURE_2D);
		
		
		while(!Display.isCloseRequested()){
			
			glClearColor(0, 0, 0, 0);
 			glClear(GL_COLOR_BUFFER_BIT);
 			
 			glMatrixMode(GL_PROJECTION);	//Loads the Projection-matrix
 			glLoadIdentity();	//Resets the projection-matrix
 			//Sets a coordinate system where one pixel on the screen equals one coordinate
 			glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);	
 			
 			glMatrixMode(GL_MODELVIEW);	//The "Camera Position Matrix"
 			glLoadIdentity();	
 			glTranslatef(Display.getWidth()/2, Display.getHeight()/2, 0);	//We want to draw with the center of the screen as (0, 0)
 			
 			glBegin(GL_QUADS);
 			{
	 			glColor3f(1, 1, 1);
	 			glTexCoord2f(0, 0);
	 			glVertex2f(-250, -250);
	 			
	 			//glColor3f(0, 1, 0);
	 			glTexCoord2f(1, 0);
	 			glVertex2f(250, -250);
	 			
	 			//glColor3f(0.5f, 0.5f, 0.5f);
	 			glTexCoord2f(1, 1);
	 			glVertex2f(250, 250);
	 			
	 			//glColor3f(0, 0, 1);
	 			glTexCoord2f(0, 1);
	 			glVertex2f(-250, 250);
 			}
 			glEnd();
			
			Display.sync(100);
			Display.update();
		}
		Display.destroy();
		JOptionPane.showMessageDialog(null, "Complete!");
	}

}

Run the file in CMD. It will say the error just like in eclipse. Most likely you don’t have the image near the jar your’e using.
If you want eclipse to export your jar with resources inside of them, create a res folder in your main project folder. I usually call it ‘res’ as most people do. Then in eclipse, goto project properties, build path, libraries, add class folder and select your newly create folder.

Now drop all images, sounds and everything you want exported with your jar into that folder. To access that folder in code, do OpenGL_Example.class.getResourceAsStream("/image.png");

@trollwarrior1
I did as you told me to and I did indeed get an error message. However I don’t quite understand what it means. Could you help me?


C:\Users\Admin>java -jar TestOpenGL.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Mathod)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
	at java.lang.ClassLoader.loadLibrary(Unknown Source)
	at java.lang.Runtime.loadLibrary0(Unknown Source)
	at java.lang.System.loadLibrary(Unknown Source)
	at org.lwjgl.Sys$1.run(Sys.java:73)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
	at org.lwjgl.Sys.loadLibrary(Sys.java:95)
	at org.lwjgl.Sys.<clinit>(Sys.java:112)
	at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
	at OpenGL_Example.main(OpenGL_Example.java:26)
	... 5 more

You need lwjgl natives.

this
http://www.java-gaming.org/topics/compiling-and-running-a-lwjgl-project/31479/view.html

or download jarsplice and pack natives into jar manually.

You may also have to call

System.setProperty("org.lwjgl.librarypath, pathtojar);

@trollwarrior1
I’ll say what the guy who made that other comment said: “You are a saint! Thank you so much!”!!
Just one new line of coad, calling that function, and all of a sudden it works (as long as you have that “nat” folder of course =)