Menu

java.lang.NoClassDefFoundError

2006-01-31
2013-06-12
  • Kedarnath Donthi

    Hi,
    I embedded some STAF commands into a Java program and executed it. It worked well. But when I created an executable jar file of the same and tried to execute it at the command prompt it popped the following error -

    Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/staf/STAFExce
    ption

    Why is it so? If at all the problem is in the classpath, the same is working when executing using "java <fileName>" command from the same place.

    Kedarnath.

     
    • Charles Rankin

      Charles Rankin - 2006-01-31

      That's just the way Java works.  When you create an executable jar, Java expects *all* the necessary classes to included in the jar.  It doesn't use the external CLASSPATH at all.  You can work around this some by creating a manifest and setting the Class-Path: entry in it to point to jars that you need.  To the best of my knowledge, these paths cannot contain any form of environment variable.  Typically you use relative paths, where the necessary jars are relative to your executable jar.

      For example, the STAX Monitor is an executable jar (STAXMon.jar) which typically resides in either <STAF Root>/bin or <STAF Root>/services/stax.  The MANIFEST.MF that we use for it is as follows.

      *** Begin STAXMon.jar MANIFEST.MF ***

      Manifest-Version: 1.0
      Main-Class: com.ibm.staf.service.stax.STAXMonitor
      Class-Path: ../lib/JSTAF.jar ../bin/JSTAF.jar JSTAF.jar ../../lib/JSTAF.jar ../../bin/JSTAF.jar

      *** End STAXMon.jar MANIFEST.MF ***

      Hope that helps.

       
    • Kedarnath Donthi

      Hi Rankin,

      I tried as you said. But this time, it said that the main class is missing though I declared the class as public. Can you please rectify my problem.

      Thankyou,
      Kedarnath.

       
    • Charles Rankin

      Charles Rankin - 2006-02-01

      Just to make sure we are on the same page... You are trying to create a Java program packaged as a jar that you will invoke from the command line along the lines of

      java -jar YourApp.jar <input arguments>

      If that's not the case, then tell me more about what you are doing and how you are trying to get things working.

      Assuming we are on the same page, then as long as you have the MANIFEST.MF file in your jar (in the the META-INF directory), and the manifest has the correct class in "Main-Class:", and the manifest has the appropriate paths in "Class-Path:" then things should work fine.  If it's not, then one of those three things is likely wrong.

      If you can't get things to work then post the output of "jar tf <Your jar>" as well as the contents of your MANIFEST.MF file.

       
    • Kedarnath Donthi

      Rankin,

      As you said, we are on the same page. I am correct with the three things you specified, yet I am unable to execute the jar. So, I am providing you with what you specified.

      Output of "jar tf <jarFile>" :
      META-INF/
      META-INF/MANIFEST.MF
      testMF.class

      Contents of MANIFEST.MF
      Manifest-Version: 1.0
      Class-Path: D:\new\testMF.class C:\staf\bin\JSTAF.jar C:\j2sdk1.4.1_01\lib\tools.jar
      Created-By: 1.4.1_01 (Sun Microsystems Inc.)
      Main-Class: testMF

      Please let me know if I am wrong anywhere.

      Kedarnath.

       
      • Charles Rankin

        Charles Rankin - 2006-02-02

        From some testing and perusal of the Java manifest specification, you can't use absolute paths in the "Class-Path:" definition.  In a simple test I did, I was getting the same error as you.  Once I made the paths relative instead of absolute, things started working.

         
    • Kedarnath Donthi

      Thanks a lot, Rankin. Its working.

      As you said, I modified the absolute path in the manifest file to relative path and now I am able to execute the jar file that I created.

      Kedarnath.

       

Log in to post a comment.