PRB: NoClassDefFoundError Accessing a COM Object Through Java (178560)



The information in this article applies to:

  • Microsoft Visual J++ 1.0
  • Microsoft Visual J++ 1.1
  • Microsoft virtual machine
  • Microsoft SDK for Java 2.0
  • Microsoft SDK for Java 2.01
  • Microsoft SDK for Java 2.02
  • Microsoft SDK for Java 3.0
  • Microsoft SDK for Java 3.1
  • Microsoft SDK for Java 3.2

This article was previously published under Q178560

SYMPTOMS

When executing a Java Application that calls a COM object like an ATL Server, you may sometimes get the following error message on the COM object:
error: java.lang.NoClassDefFoundError:
The error occurs on the line where you create a new COM object in the Java Class.

CAUSE

One reason why you may get this error could be related to missing parameter attributes for the methods in the COM object's IDL file.

RESOLUTION

Check your methods in the IDL file so that they have the right attributes like [in],[out],[retval] and so on. Once modifying the IDL file, rebuild your COM Server, and rerun JavaTLB or JACTIVEX on the COM Object's Type Library so that the changes gets reflected.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a default ATL COM object using VC5.0. Name it AtlServ.
  2. Add a new ATL COM object to the above project. This can be done by right-clicking in Class View and selecting New ATL Object. This will bring up the ATL Object Wizard. Select Simple Object in it and click Next. Now select the Names Tab and name the object as Test.
  3. Add a method to the IDL file as follows:
    ........
    interface ITest : IDispatch
    {
       [id(1), helpstring("Hello")] HRESULT TestMethod(BSTR* strGUID);
    };
    ..........
    						
  4. Add declaration/implementation to this method in the .cpp and .h files of your Test ATL COM Object as follows. Declare this method in the Test.h file as follows:
    .............
    public:
    STDMETHOD(TestMethod)(BSTR* a);
    
    						
    Following are its implementation:
    // Test.cpp : Implementation of CTes
    ..................
    STDMETHODIMP CTest::TestMethod(BSTR* a)
    {
       USES_CONVERSION;
       *a = SysAllocString(T2OLE("This is a test!"));
       return S_OK;
    }
    						
  5. Now build the ATL Server object, which should also register it.
  6. Run JAVATLB or JACTIVEX on the server you created in Step 1. If you are using JACTIVEX, then you will have to use JVC version 4337 or greater that ships with the SDK2.0 and 2.01 for Java in order to build your Java applet.
  7. Create a new Java project using Visual J++ 1.1 and create a new class as follows:
    import atlserv.*;      //This is your ATl Server Object 
                           //created in Step 1.
    
    public class Main
    {
       public static void main(String args[])
      {
          ITest a = (ITest) new Test();
          String result[]=new String[1];
          a.TestMethod(result);
          System.out.println(result[0]);
    
      }
    }
    						
  8. Build the class and copy it to <windir>/java/lib. where <windir = WINNT or WIN95>.
  9. When running this application using JVIEW you will get the NoClassDefFoundError in the Command-Prompt window. (NOTE: when using SDK for Java 3.0 and newer you will get 'null' returned.)
  10. To fix this problem, specify the [out] attribute to the method in the IDL file as follows:
    interface ITest : IDispatch
    {
       [id(1), helpstring("Hello")] HRESULT TestMethod([out]BSTR*
        strGUID);
    };
    						
  11. Now rebuild your ATL server, and rerun JAVATLB or JACTIVEX on the ATL Server's Type Library so that the changes gets reflected.
  12. You should now be able to run your Java application without any problems.

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbprb KB178560