Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird ClassNotFoundException in Java
Hi, this question is mainly aimed at @tomaszkax86 as I know he has some experience with linking lwjgl libraries, but if anybody knows the solution to my problem please do answer Smile

I am currently using ant to compile my source code which uses lwjgl for opengl and slick-utils for texture loading.
I believe I have my build script set up properly as I am able to compile with lwjgl alone.

The weird thing is that even though I appended slick-util.jar to my classpath(which already contains lwjgl jars), the code compiles ok, but throws a ClassNotFoundException followed by a NoClassDefFoundError for a single class file (ResourceLoader) during runtime .
The exception is only thrown at runtime and only for that single class(all other classes from slick-util.jar work Huh or at least do not throw any exceptions yet).

What I already checked:
- classpath in the build script contains slick-util.jar
- the script correctly copies the jar file to lib directory used by my application
- the jar file contains ResourceLoader.class in a correct path
- my imports in the source code are correct

I have no idea what else could go wrong. Any ideas anybody?
Complete or partial stack trace could help a lot. You don't need to include source code if you don't want to, although I need to know what specific class and method cause this exception.
"After three days without programming, life becomes meaningless."
~The Tao of Programming

     [java] libGL error: dlopen /usr/lib/fglrx/dri/ failed (/usr/lib/fglrx/dri/ cannot open shared object file: No such file or directory) <- ignore that, opengl wants to use my discrete gfx card which is currently disabled
     [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/newdawn/slick/util/ResourceLoader
     [java] at ImageAtlas.<init>(Unknown Source)
     [java] at MainJump.main(Unknown Source)
     [java] Caused by: java.lang.ClassNotFoundException: org.newdawn.slick.util.ResourceLoader
     [java] at$
     [java] at$
     [java] at Method)
     [java] at
     [java] at java.lang.ClassLoader.loadClass(
     [java] at sun.misc.Launcher$AppClassLoader.loadClass(
     [java] at java.lang.ClassLoader.loadClass(
     [java] ... 2 more
     [java] Java Result: 1
I'm on Ubuntu 14.04 and latest java 1.7
The snippet of code which uses the class ResourceLoader:
import org.newdawn.slick.util.ResourceLoader;          //used here
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


public class ImageAtlas{

    private Texture texture;

    ImageAtlas(String path){


            texture = TextureLoader.getTexture("PNG",
                ResourceLoader.getResourceAsStream(path)); //and here
        catch(IOException e){

            System.out.println("Could not load texture");
getResourceAsStream(String) is the only method I'm using from ResourceLoader
Let me know if you need any more info.
(01-11-2015, 07:18 PM)CHmSID Wrote: - the script correctly copies the jar file to lib directory used by my application
Are you 100% sure it actually loads the file from this location?
Quote:Are you 100% sure it actually loads the file from this location?

my dev directory contains three directories: source, lib and data.
When I build the project, a new directory is created called dist where both data and lib directories are copied to with their content, then a jar file is also generated there.
By doing this, I know that the dist folder will contain the exact same files that are in my development directory.
I also know that by running the "run" task, I'm not in the same directory as the jar file I'm trying to run. But because the folder structure is the same and the contents are exactly the same, I don't think it matters.

I'm pretty sure the jar file containing the library is loaded at the very least during the compilation, because there are no errors. I also tried to set up the classpath for runtime manually and run the jar from it's directory but it also didn't work.

The fact that only one class out of entire library could not be found throws me off...
This is a very strange error. Have you tried creating empty project and running just this class's method? I downloaded newest library and executed ResourceLoader.getResource() method alone with some argument and it worked fine.
"After three days without programming, life becomes meaningless."
~The Tao of Programming
Solved. Sorry for all the trouble, it's the little detail that's always the culprit Sad
I had the class path set up correctly for the jar creation, but I forgot to include the library jar path in the I was copying into the jar Sad

Embarrassing, sorry Tongue

I came to the solution by comparing my jar with the jar created by JarSplice(which worked), and I noticed the said lib is missing...

edit: how do I give you reputation for helping me?
Go to one's profile and click "Rate". Also, remember that giving like is like giving 1 reputation point. You can give maximally 5 reputation points a day as I remember.
[Image: XvN5CTW.png] [Image: UYXyyMS.png]
It's OK, we all make embarrassing mistakes sometimes. The one I remember well is that I forgot one time that Class-Path attribute in manifest needs a space separated list of files, not semicolon separated like the one in -classpath switch. Took me a while to figure out what was wrong.

Since you are using Ant to compile your project, I can tell you about a nice trick. You can create manifest attributes directly in build.xml. Useful if you have few attributes because you don't have to make manifest file outside. Put something like this into <jar> task:

<attribute name="Main-Class" value="program.MainClass" />
<attribute name="Class-Path" value="File.jar OtherFile.jar" />

More info here:
"After three days without programming, life becomes meaningless."
~The Tao of Programming
Thanks, makes my directory a little bit cleaner now

Forum Jump:

Users browsing this thread: 1 Guest(s)