1

I have some simple code written by another group of people (presently unavailable), which references some jar files that I was given. When I compile the code everything builds fine, but as soon as the code tries to create an instance of a class defined in one of the jar files I get a java.lang.NoClassDefFoundError. I did a fair amount of research looking into this problem, and everything pointed to an issue with my classpath at run-time. However, when I print the classpath at run-time, I see all the jar files, and they seem to have the correct path. The run-time classpath also seems to match the compile-time classpath, so this doesn't seem to be the issue.

One thing I should mention: I'm using JDK 1.8, while I believe these jar files were created using JDK 1.7. None of the other solutions I saw mentioned this, but is there any chance this is a version compatibility issue? I haven't been able to obtain JDK 1.7 through an approved method, but if I can convincingly prove this will fix my problem I may be able to get the older version approved for install.

If it can't possibly be a version issue, where should I go from here? Every answer I've found doesn't seem to apply to my situation, but if you can point to one I may have missed or have a solution I would be very appreciative. NetBeans-specific answers are helpful, but not required. Thanks!

EDIT: For reference, here it the code I'm using to print the classpath at runtime:

    ClassLoader cl = ClassLoader.getSystemClassLoader();

    URL[] urls = ((URLClassLoader)cl).getURLs();

    for(URL url: urls){
        System.out.println(url.getFile());
    }
ping
  • 39
  • 5
  • 1
    What's the missing class, and how do you "print the classpath"? – chrylis -cautiouslyoptimistic- May 14 '19 at 23:46
  • See the edit in the main question for my code used to print the classpath. The missing class is part of a jar file provided by a third part.y – ping May 14 '19 at 23:48
  • I've encountered this before, it was due to some exception in static initializer -- you can see some examples @ https://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html -- EDIT: it's one of the possible causes – nevets1219 May 14 '19 at 23:59
  • 1
    Please share the entire stacktrace if possible. – nevets1219 May 15 '19 at 00:00
  • Is this a CLI program or running in some sort of container? – chrylis -cautiouslyoptimistic- May 15 '19 at 00:11
  • Another possibility is if your external .jar is defined as a *relative* path, and cannot be resolved based on the *working dir*. [Have you checked what the working directory is at runtime](https://stackoverflow.com/a/7603444/5623232)? (which may be different to the compile-time one) – NPras May 15 '19 at 04:18

1 Answers1

0

Please make sure your compile time and run time code took the same version of jar files.If the version conflict of jar exists, you might encounter above problem

indhu
  • 56
  • 5