1

I'm sorry that this may seem like a basic question, but I need help on importing classes. I downloaded the Lightweight Java Gaming Library(LWJGL), but I don't know how to install it. When I try the test, it doesn't work.

I have a folder in which I extracted the basic LWJGL files. I now have a setup like this:

generic_folder/libs/lwjgl-2.8.3

I also have this test which I copy-pasted from the tutorial on the LWJGL wiki: It is located in the generic_folder. When I try and javac it, it says DisplayExample.java:1: package org.lwjgl does not exist.

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class DisplayExample {

public void start() {
    try {
    Display.setDisplayMode(new DisplayMode(800,600));
    Display.create();
} catch (LWJGLException e) {
    e.printStackTrace();
    System.exit(0);
}

// init OpenGL here

while (!Display.isCloseRequested()) {

    // render OpenGL here

    Display.update();
}

Display.destroy();
}

public static void main(String[] argv) {
    DisplayExample displayExample = new DisplayExample();
displayExample.start();
}

}

Do I have to place the files in a certain location or what? Please help. Thanks!

Edit: I am sorry. I'm new to Java. Please go a little slower. Also, LWJGL is a library, so I didn't place any files inside of it.

Impmaster
  • 187
  • 2
  • 10

5 Answers5

4

You need to specify a classpath (preferrably with the -cp switch) such that java/javac knows where to find the .jar files you need access to. It typically looks as follows:

For compiling:

javac -cp .:libs/lwjgl-2.8.3/jar/lwjgl.jar your-java-files

for running:

java -cp .:libs/lwjgl-2.8.3/jar/lwjgl.jar your-class-file

(Note that if you're using Windows, I believe : should be replaced by ;.)

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

You need to add that jar to your classpath when you invoke javac, like so:

javac -cp generic_folder/libs/lwjgl-2.8.3.jar path/to/java/file/DisplayExample.java

I'm assuming that lwjgl-2.8.3 is actually a jar file. You never specified in your question.

You'll have to once again add the jar to your classpath when you attempt to run your example:

java -cp generic_folder/libs/lwjgl-2.8.3.jar path/to/java/file/DisplayExample
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
  • Woops, forgot about that. lwjgl-2.8.3 is actually a folder with several different folders inside of it. I know where to locate the correct jar file, though. – Impmaster May 09 '12 at 12:20
1

The easiest way is to set the LWJGL folder to CLASSPATH environment. For example

set CLASSPATH=${PATH_TO}/generic_folder/libs/lwjgl-2.8.3

That should do it.

toy
  • 11,711
  • 24
  • 93
  • 176
  • I wouldn't rely on environment variables. It makes the build process dependent on the system-configuration which might get quite confusing. – aioobe May 09 '12 at 12:09
0

When you run javac, you need to tell it where the jar file is. You can either set you CLASSPATH environment variable or pass the location to the java compiler. The latter is better, as you probably won't want lwjgl if you start a second java project.

To tell the java compiler directly, you would do something like

javac -classpath generic_folder/libs/lwjgl-2.8.3/lwjgl.jar DisplayExample.java

The classpath can either point to a jar file containing the classes or the top level directory containing unjared class files. If the unjarred classes are in generic_folder/libs/lwjgl-2.8.3/classes, you would point to generic_folder/libs/lwjgl-2.8.3/classes, not generic_folder/libs/lwjgl-2.8.3/org/lwjgl

Troy Daniels
  • 3,270
  • 2
  • 25
  • 57
0
javac -classpath external.jar myClass.java

For more info -> https://stackoverflow.com/a/5112638/5790669

Yash P Shah
  • 779
  • 11
  • 15