5 Replies Latest reply: Mar 7, 2013 3:51 AM by alley RSS

    NoClassDefFoundError

    alley
      Good Morning,

      I have created a jar file which works fine on my pc at home.

      However, when I run it on another pc, it gives me this error:
      Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/exc
      eptions/InvalidPasswordException
              at java.lang.Class.getDeclaredMethods0(Native Method)
              at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
              at java.lang.Class.getMethod0(Unknown Source)
              at java.lang.Class.getMethod(Unknown Source)
              at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
              at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
      Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.exceptions.Invali
      dPasswordException
              at java.net.URLClassLoader$1.run(Unknown Source)
              at java.net.URLClassLoader$1.run(Unknown Source)
              at java.security.AccessController.doPrivileged(Native Method)
              at java.net.URLClassLoader.findClass(Unknown Source)
              at java.lang.ClassLoader.loadClass(Unknown Source)
              at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
              at java.lang.ClassLoader.loadClass(Unknown Source)
              ... 6 more
      Do you know what could be the problem? Is it something to do with the classpath? And if yes, isn't a jar file supposed to run as a standalone program on every pc with jdk installed without needing to set classpath etc...?

      Thanks for your help.
        • 1. Re: NoClassDefFoundError
          gimbal2
          Your application has "external dependencies", other jars that need to be on the classpath. You can easily see this by opening up your application jar and then opening META-INF/MANIFEST.MF in a text editor. It will contain a line 'class-path' which lists all the jars your application needs, and where it will look for them. A general design pattern is that they go in a 'lib' subdirectory.

          So yeah - you forgot to copy the other jar(s) needed.
          • 2. Re: NoClassDefFoundError
            alley
            Hi gimbal,

            Thanks for your explanation.

            These are the contents of the manifest:
            Manifest-Version: 1.0
            Ant-Version: Apache Ant 1.8.3
            Created-By: 1.7.0_13-b20 (Oracle Corporation)
            Class-Path: lib/pdfbox-1.7.1.jar lib/fontbox-1.7.1.jar lib/commons-log
             ging-1.1.1.jar lib/jna-3.5.1.jar lib/platform-3.5.1.jar
            X-COMMENT: Main-Class will be added automatically by build
            Main-Class: renamepdf.RenamePDF
            So if I understand correctly, I should download these jars: pdfbox-1.7.1.jar, fontbox-1.7.1.jar, commons-log
            ging-1.1.1.jar, jna-3.5.1.jar, platform-3.5.1.jar

            And where should I put them?

            Also, isn't a jar file supposed to work on its own without further intervention? What if I need to give the jar file to different people, I have to install all these jars on their pc's?

            Thanks very much!
            • 3. Re: NoClassDefFoundError
              gimbal2
              you should already have them - in the machine where the jar was built.
              And where should I put them?
              As you can see, the class-path refers to a 'lib' subdirectory as I suspected. So you would distribute the application as:

              app.jar
              lib/pdfbox-1.7.1.jar
              lib/fontbox-1.7.1.jar
              etc.

              You have to be careful how you run it. Java will look for the libraries relative to the working directory, which is the directory where the actual java or javaw command is executed. This is no issue when you run the jar by just double clicking on it / on a shortcut to it.

              This is also nothing unusual, any native application comes with a big pile of additional files next to the executable, including a pile of DLL files which you can lazily compare to a jar.
              isn't a jar file supposed to work on its own without further intervention?
              That refers to being able to double click on the (executable) jar file in an OS gui and the application then running. You still need to package and deploy it properly. Which means you need to know how to package and deploy it properly.
              I have to install all these jars on their pc's?
              You could...

              - just copy them with the jar; zipping it all up works wonders
              - create an installer, which is the professional solution applied to nearly all software in existence
              - use webstart
              - use a onejar solution to cram everything into one huge jar; you have to be careful with this because you may be breaking licenses depending on what jars you use.
              • 4. Re: NoClassDefFoundError
                alley
                Hi gimbal,

                Thanks for your very detailed explanation! :-)

                Will try it out.

                As regards the installer, I'm not experienced on that, but will check out some tutorials on the net.

                Cheers & good day.
                • 5. Re: NoClassDefFoundError
                  alley
                  Hi,

                  Following your explanation, it worked now.

                  Thanks very much for your help.

                  Cheers.