views:

457

answers:

2

i'm trying to compile this slick2d example (first one) but i can't get it to work. here's the code:

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
/**
 * @author panos
 */
public class WizardGame extends BasicGame
{
    public WizardGame()
    {
        super("Wizard game");
    }

    public static void main(String[] arguments)
    {
        try
        {
            AppGameContainer app = new AppGameContainer(new WizardGame());
            app.setDisplayMode(500, 400, false);
            app.start();
        }
        catch (SlickException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void init(GameContainer container) throws SlickException
    {
    }

    @Override
    public void update(GameContainer container, int delta) throws SlickException
    {
    }

    public void render(GameContainer container, Graphics g) throws SlickException
    {
    }
}

i've put lwjgl.jar, slick.jar and ibxm.jar in the directory as instructed. then i tried:

javac -classpath .;full path\slick.jar.;full path\lwjgl.jar;full path\ibxm.jar

then

java WizardGame

but i get errors that the jar classes aren't defined, which means something must be wrong when i'm setting the classpaths. i'm not sure what i'm doing wrong though.

i've also tried

javac -classpath .;.\slick.jar.;.\lwjgl.jar;.\ibxm.jar

either way, shouldn't the jvm find out about the .jar files on its own if they're on the same directory as the source code? in my case, i've set up the 3 jars in the same directory as WizardGame.java. if i try compiling without the -classpath arguments it doesn't work though.

edit: i did as instructed, but now it's telling me "no lwjgl in java.library.path"

i examined my lwjgl.jar with winrar and saw that it contains two folders: meta-inf and org, and then inside org there is lwjgl. is it supposed to be like that? because if it is, i dunno what's wrong.

edit 2: created the manifest with what you said, then used:

jar cfm test.jar manifest.txt

this created a jar file, then i used

java -jar test.jar

and then it told me it didn't find the definition for the class WizardGame.

+5  A: 

You have to add the -classpath option to the java command also. Not only to the compiler.

like:

javac -classpath .;lib\myjar.jar MyClass.java

and then

java -classpath .;lib\myjar.jar MyClass

Otherwise the interpreter ( java ) won't know where to find the classes needed.

OscarRyz
+4  A: 

Oscar is bang on - you need the -classpath on the java command too.

Another thing you can look at (it is overkill for what you are doing now, but you should still do it for the experience) is to create a JAR file. In the JAR file you can provide a manifest.mf file that tells the java command where to find the other JAR files as well as what the main class is.

You would then run your program via "java -jar foo.jar"

  • http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
  • http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html
  • http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html

I believe that the following manifest.m file will do what you want (note you have to have a blank line at the end or it will not work):

Main-Class: WizardGame
Class-Path: slick.jar lwjgl.jar ibxm.jar

That assumes that all JAR files (foo.jar and the other three) are in the same directory.

TofuBeer
Probably as "advanced topics" but is worth to learn this since the beginning
OscarRyz
For actual distribution, this is the better answer.
Software Monkey