How to run lwjgl Programms outside the IDE

Started by PinkieSwirl, September 13, 2010, 15:16:32

Previous topic - Next topic

PinkieSwirl

Hello there,

my problem is, that when i build the code, i can't run the created jar file.

I'm using NetBeans and i have imported lwjgl and i can run it with NetBeans.

I have read other topics with the same problem, but i don't know how to "import" the natives to the library. NetBeans creates a folder "dist" with the jar file and a folder "lib". In the "lib" folder are only the lwjg jar files.... i have copy and paste the natives in this folder but how can i java "say" they'r in there?

Please dont aswer like "-Djava.library.path.=", cause i don't know what i have to do with that.

Hope you understand me, cause english isn't my native language.

Evil-Devil

You have to attach the "-Djava.library.path" to the java executable.

On a windows system you might create a batch file like:

java -Djava.library.path=lwjgl/ -classpath
JavaLWJGL.jar;lwjgl/lwjgl.jar;lwjgl/lwjgl_util.jar
com.evildevil.tutorials.java.lwjgl.JavaLWJGL


Djava.library.path takes your path to the native files. So when your natives can be found within your lib folder, then you only have to say:
-Djava.library.path=lib/

And then you tells the java classpath which jars it have to use. Finally you call the classfile that containts your main method.

Hope that helps :)

PinkieSwirl

Ok, i created a batch file:

java -Djava.library.path=lib/ -classpath
Spaceinvaders.jar;lib/lwjgl.jar;lib/lwjgl_util.jar;lib/jinput.jar;lib/lwjgl-debug.jar;lib/lwjgl_test.jar;lib/lwjgl_util_applet.jar
spaceinvaders.Game

-Djava.library.path=lib/ // here are the natives (for all Systems)
Spaceinvaders.jar // this is the runnable jar file
lib/lwjgl.jar;lib/lwjgl_util.jar;lib/jinput.jar;lib/lwjgl-debug.jar;lib/lwjgl_test.jar;lib/lwjgl_util_applet.jar // this are the library jar files
spaceinvaders.Game // this is the main Class

It starts the application but it do nothing when i choose lwjgl (i have a optiondialog where you can choose if the game is rendered with java2d or lwjgl)
there is no difference (java2d run, lwjgl : lwgl in java.library.path not found)

Did i understand something wrong?

The files that are in the Spaceinvaders.jar file:
\META-INF\MANIFEST.mf
\spaceinvaders\...(class files)
\spaceinvaders\java2d\...(class files)
\spaceinvaders\lwjgl\...(class files)
\spaceinvaders\util\...(class files)

The files that are in the lib folder:
lwjgl.jar
lwjgl_util.jar
jinput.jar
lwjgl-debug.jar
lwjgl_test.jar
lwjgl_util_applet.jar
and the dlls, sos ... (the natives for all Systems)

And is there a way to run it on all Systems (cause only a batch file for windows.. whats with the other Systems?)

mstWeal

I could be wrong but I guess Windows doesn't like the forward slash with -Djava.library.path here. So instead lib/ try to use lib\
Also, I would recommend to externalize the classpath and main class stuff to a separete MANIFEST.MF file in a folder called META-INF at the root of your project. Also, I think you forgot the program argument -jar that is necessary to execute Java jar files.

As an example, here is how my batch / manifest looks like:

javaw -Djava.library.path=lib\native\windows -jar game.jar

The game.jar is in the same folder as the batch file in this case. Do note that it's important that -Djava.library.path must occur before -jar (that got me some sleepless nights and grey hair).

Manifest-Version: 1.0
Class-Path: lib/jar/lwjgl.jar lib/jar/lwjgl_util.jar lib/jar/j-ogg.jar
Main-Class: org.sunstormstudios.demo.game.DemoGame

As to your other question to run a jar on all systems. There is no way to do that unfortunately. Basically it works, for you can run the jar on all systems with the java -jar console command. But you need to ship different libraries for those systems (it doesn't make sense to include the Linux libraries in case you deploy for Windows, that only increases download size). And what you have to provide for the different systems are the "convenience starters". Like for a normal user it's not acceptable to open up a console and type java -jar or something. For this you can provide the batch file but that is Windows-specific. What would even be better is a small C program that starts the JAR and is compiled to a .exe file as that's the standard way of starting programs on Windows. But for other systems, this is different. There is no way around that.

PinkieSwirl

Ok, that was it, now it run the application

my batch file:
java -Djava.library.path=lib/ -jar Spaceinvaders.jar -classpath
lib/lwjgl.jar;lib/lwjgl_util.jar;lib/jinput.jar;lib/lwjgl-debug.jar;lib/lwjgl_test.jar;lib/lwjgl_util_applet.jar
spaceinvaders.Game

but the last line seems to be not impotant?
when i delete it it changes nothing.

all the files are in the folder "dist":
dist/lib/..
dist/batch.bat
dist/Spaceinvaders.jar

How can i change the manifest file, cause when i open the jar file with a zip programm, i can open the manifest file, but i can't change it.
I create the jar file with NetBeans, so i dont know how to create one by myself

So, are .bat and .mf the "same" ? Means when i can change the manifest file i dont need the batch file?

Thanks for the help so far :)

Quote from: mstWealAs to your other question to run a jar on all systems.
Thanks

Evil-Devil

As you run an jar file the last line is indeed obsolete as the main class is defined in your manifest.

To edit the manifest file you can just extract it, edit it and reinsert it. A plain jar file is nothing else than a zip file.

Matthias

When you use Netbeans you will see a manifest.mf in your project folder. Edit this file and it will be included on built.
Also you don't need to manually include the classpath or main class attributes as they will be added by Netbeans - at the same time Netbeans will also copy all dependencies into dist/lib/.
So in order to get it to work you just need to reference the dependencies as JAR files. For external sources just make another library project, add the sources and put this 2nd project as a dependency for the 1st.