4

I'm trying to compile and run a very basic program that uses LWJGL:

import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; public class HelloWorld { public static void main (String args[]) { try { Display.setTitle("Hello World"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); } while (!Display.isCloseRequested()) { try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } } } 

I managed to compile it using:

javac -classpath ~/Downloads/lwjgl-2.8.3/jar/lwjgl.jar:~/Downloads/lwjgl-2.8.3/jar/lwjgl_util.jar:~/Downloads/lwjgl-2.8.3/jar/jinput.jar HelloWorld.java 

But now I can't run it... I tried:

java HelloWorld 

And:

java -Djava.library.path=~/Downloads/lwjgl-2.8.3/native/linux HelloWorld 

But none of those works. Both of them complain that LWJGLException class definition was not found.

I am running Linux, and I am not using an IDE such as Eclipse or Netbeans. I don't want to use one, I want to be able to run from terminal.

1
  • an idea it is to set up an IDE and catch his compile command and the run command. A couple of years ago or the Netbeans or the Eclipse did print out that command what you need. Save that 2 command to a 2 sh file command.sh and run.sh than play it without IDE Commented Sep 8, 2012 at 16:44

3 Answers 3

6

The following works on my Windows machine, but I've adapted the shell commands for linux formatting (colons vs. semi-colons):

Set up a directory structure as such:

/HelloWorld.java /lib/jwjgl.jar /lib/jinput.jar /lib/jwjgl_util.jar /native/linux/... (all your native files) 

Compile:

From your shell, navigate to the parent directory containing HelloWorld.java, and type the following:

javac -cp .:lib/* HelloWorld.java 

-cp specifies that the .java and .class files to compile your program can be found within both . (the current directory) and any jar file under lib/. Note that you could manually specify the .jar files, i.e. -cp .:lib/lwjgl.jar:lib/jinput.jar etc, but * (java 1.6+ only, I believe) is a shortcut to specify all jars in a directory.

Run:

Now run the following command from the parent directory:

java -cp .:lib/* -Djava.library.path=native/linux HelloWorld 

Again, -cp specifies that your compiled .class files can be found in the current directory and within any jars under the /lib directory. -Djava.library.path= specifies where your native files can be found. Note that you did not put a leading / in front of native. By omitting the leading /, you're telling java that the native directory is a subdirectory relative to the current working directory. If you accidentally include the /, it will treat native as an absolute directory, which is probably not what you want.

It's perfectly acceptable to specify a location for the native files outside of the current working directory. To do so, you'll have to provide the absolute location, which on Windows would be, for example:

-Djava.library.path=C:\jwjgl-2.8.4\native\windows 

That should be all you need to get up-and-running without an IDE or build script!

Final Note

The HelloWorld.java, as written, behaves poorly (the screen locks up and you must force-close the process). Try the following code (adapted from multiple source across the web, with minor modifications to suit this example, but primarily not of my own effort), as a replacement for HelloWorld.java. Enjoy!

import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.input.Keyboard; public class HelloWorld{ public void start() { try { Display.setDisplayMode(new DisplayMode(640, 480)); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } // Init OpenGL GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(-3.2, 3.2, -2.4, 2.4, -1, 1); GL11.glMatrixMode(GL11.GL_MODELVIEW); boolean quit = false; while (!quit) { // Clear the screen. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Begin drawing GL11.glBegin(GL11.GL_TRIANGLES); // Top & Red GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glVertex2f(0.0f, 1.0f); // Right & Green GL11.glColor3f(0.0f, 1.0f, 0.0f); GL11.glVertex2f(1.0f, 1.0f); // Left & Blue GL11.glColor3f(0.0f, 0.0f, 1.0f); GL11.glVertex2f(1.0f, -1.0f); GL11.glEnd(); Display.update(); if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) quit = true; } Display.destroy(); } public static void main(String args[]) { HelloWorld application = new HelloWorld(); application.start(); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

you are missing some .jar in classpath. You can add with -classpath but take care about extra signs at linux. Very probably you will need those at launch too:

java -classpath ~/Downloads/lwjgl-2.8.3/jar/lwjgl.jar:~/Do... -Djava.library.path=~/Downloads/lwjgl-2.8.3/native/linux HelloWorld 

I am not sure about the ~ sign, try to put there absolute path.

1 Comment

The ~ sign is probably working, but just in case, I also tried /home/david/. None of them worked. When I ran java -classpath ... -Djava.library.path=... HelloWorld, I got Could not find or load main class HelloWorld" I tried with and without the Djava.library.path too.
0

Try to make a complie.sh with javac command, set all .jars what you need in classpath and add all your classes, what you need to compile. This should result one or more .class files in output directory. At that point you know you hava compiled your "project".

After this you can launch with java, but launching with java has 2 modes:

java -jar yourJarName.jar 

for this you need to jar all your classes into jar file and add all required other jars in the maifest file, or you will extract all required jars and embed into your jars they classes

and the other one is like what you have tried

java -classpath your1.jar your2.jar toon of classes in claaspath com. yourpackage.MainClass

For exactly how to proceeed, pls search for "compile java project in command line Linux" and "run java application from command line linux"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.