0

Referenced Libraries Structure: (what I did manage to do, runs fine in IDE) enter image description here

StackTrace:

Exception in thread "main" java.lang.NoClassDefFoundError: org/newdawn/slick/SlickException
    at com.gametest.game.GameLauncher.main(GameLauncher.java:61)
Caused by: java.lang.ClassNotFoundException: org.newdawn.slick.SlickException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

Basically I am using slick and LWJGL to get the audio working, within the IDE it runs fine (build path configured, natives for lwjgl selected), but when compiled and ran, it crashes and in the console it gives above stacktrace.

I am running 64-bit macosx, code written and compiled in eclipse. Libraries used: lwjgl, slick (http://slick.ninjacave.com/)

ran from "Terminal" console using:

java -cp ~/Desktop/FirstGame.jar com.gametest.game.GameLauncher

This seems to be a common bug, but after a lot of time searching on the web, I have not come across an answer that solved it.

(edit) Relates to: ERROR!! AppGameContainer java.lang.ClassNotFoundException

Community
  • 1
  • 1
AgentM
  • 406
  • 4
  • 18

4 Answers4

1

I think that you've got a problem with third-party libraries which are not included into your jar (but they can be found in your IDE project's classpath, that's why it works there).

You can place any third-party jars into the /lib directory next to the jar and use -cp JVM argument. Or you can try using tools like http://one-jar.sourceforge.net/, "maven-shade-plugin" plugin for Maven build tool and so on

Cootri
  • 3,806
  • 20
  • 30
  • So basically what you are saying is, the IDE "knows" where to find these classes, but it doesn't automatically include them in the compiled product? Because it seems strange that the IDE doesn't automatically figure, you would want these libraries in your finished product. Why does it not automatically add these libraries into the jar file? is there a reason for that? – AgentM Mar 15 '16 at 19:33
  • Yes, IDE can find dependencies because you can configure classpath for the project and add jars into it. It is a long story from 90s and every beginner has headache exactly like yours :) Problem is that Java does not have any "super jar" specification for programs distribution with all necessary jar dependencies inside one archive. So you must use workarounds with external jars or special build tools, which try to solve this problem by different approaches – Cootri Mar 15 '16 at 21:00
1

Does FirstGame.jar a one-jar with all the lib under it ? if not, you will need to include all the required jar files to be added in classpath.

Something like java -cp ~/Desktop/FirstGame.jar;lib/*" com.gametest.game.GameLauncher

Nrj
  • 6,723
  • 7
  • 46
  • 58
  • java -cp "~/Desktop/FirstGame.jar;lib/*" com.gametest.game.GameLauncher did NOT work: "Error: Could not find or load main class com.gametest.game.GameLauncher" java -> javac (compiling) gives: "error: Class names, 'com.gametest.game.GameLauncher', are only accepted if annotation processing is explicitly requested 1 error" So I am out of luck there too. Is there an option in the IDE that lets you add libraries into the classpath? or do you require to do this from the commandline? – AgentM Mar 15 '16 at 19:37
  • the 'java -cp ~/Desktop/FirstGame.jar;lib/*" com.gametest.game.GameLauncher' you gave me was incorrect. 'java -cp ~/Desktop/FirstGame.jar:lib/* com.gametest.game.GameLauncher' at least ran the game, and then crashed, instead of giving me an error within the terminal commandprompt. my question then is: what does the "lib/*" mean? is that within the jar, next to the jar? Please give me the file structure, where to place the lib folder etc. – AgentM Mar 15 '16 at 19:51
  • @AgentM /lib should be a folder (inside ~ or use a qualified path if its not there) which should contain all the jar file your game needs (all 3rd party + your own if any). The other , vs : is unix vs windows convention , its good you resolved it. And yes you need to run this from command line (i guess that what you want, inside eclipse it already runs, right ?) – Nrj Mar 16 '16 at 20:32
0

Looks like eclipse couldn't find org.newdawn.slick.SlickException class that is included in Slick. Or you didn't add the Slick.JAR file to the proper place, or your classpath isn't set correctly.

try adding this slickJar in your build path it should solve your problem

Coding Otaku
  • 453
  • 7
  • 17
0

I DID IT (I am so proud of myself, I figured it out myself, using a few documentations and some of my brain)

sometimes, you need a little push from somebody to work yourself to the top.

I basically fixed it by going into MANIFEST.MF and adding a line: Manifest file

as well as moving all the jar files (and the binaries) in a single folder called "Game"

When done correctly it looks something like this: The folder structure

AgentM
  • 406
  • 4
  • 18