Home : J-Integra for COM : HOWTO : Accessing .NET Components From a Java Client
Q78813 - HOWTO: Accessing .NET Components From a Java Client

Accessing .NET Components From a Java Client

This step-by-step example shows you how you can use J-Integra to access .NET components from a Java client or embed a .NET form in a Java AWT/SWING frame. If your Java client does not embed a .NET GUI component in a Java frame, you can also use J-Integra for .NET to integrate your Java/J2EE and .NET components. J-Integra for COM uses a COM Callable Wrapper to bridge Java and .NET, whereas J-Integra for .NET uses pure .NET Remoting Protocol to do the bridging.

Contents

  1. Introduction
  2. Configure the .NET environment
  3. Compile and register the C# component
  4. Add the C# component to the GAC
  5. Create a TLB from the C# component
  6. Generate and compile the Java proxies for the C# component
  7. Compile and run the Java client

This example was built using the following:

1. Introduction

This example demonstrates how to access a .NET component without embedding it in a Java frame. To embed the .NET component in a Java frame, you'll just need to select one extra com2java option as mentioned in step 6. Generate and compile the Java proxies for the C# component, and modify the Java client as mentioned step 7. Compile and run the Java client.

To run the example, follow these directions:

  • Download the attachment below (dotnetExample.zip).
  • Unzip it into any folder of your choice.
  • Open run.bat and modify the environment variables to reflect your environment. Save the file when finished.
  • Double-click run.bat - this will perform all the necessary steps to configure, compile, and run the example.

We will now walk you through all the steps in run.bat to have a closer look at what is going on. We will skip over the first two sections - Deleting old files and Setting up environment variables - as they are self-explanatory.

2. Configure the .NET environment

  call "%DOTNET_HOME%\Vc7\bin\vcvars32.bat"

This command configures the .NET environment in your current window so you can use the .NET tools later on.

3. Compile and register the C# component

  csc /out:compcs\CompCS.dll /target:library compcs\CompCS.cs
regasm compcs\CompCS.dll

The csc command compiles the C# code, and the regasm command registers the component.

4. Add the C# component to the GAC

  gacutil /u CompCS
gacutil /i compcs\CompCS.dll

Use the gacutil command to add the component to the GAC (Global Assembly Cache).

5. Create a TLB from the C# component

  tlbexp compcs\CompCS.dll /out:compcs\CompCS.tlb

Use the tlbexp command to generate a TLB so that the .NET component can be accessed froma COM client.

6. Generate and compile the Java proxies for the C# component

Non-GUI .NET Component

  set TYPELIBS=
set TYPELIBS=%TYPELIBS% /ExternalTypelib c:\WINNT\system32\stdole2.tlb=stdole2
set TYPELIBS=%TYPELIBS% /ExternalTypelib c:\WINNT\Microsoft.NET\Framework\v1.1.4322\mscorlib.tlb=mscorlib
set FLAGS=/ClashPrefix zz_ %TYPELIBS%
com2javacmd %FLAGS% .\compcs\CompCS.tlb compcs .\compcs
javac compcs\*.java

The above commands will generate Java proxies for CompCS.tlb. The CompCS.dll component references certain types found in two external type libraries (as do all .NET components. Therefore, the /ExternalTypelib flag is needed to tell the com2java tool what Java package the proxies for these external types belong to.

In the example, the proxies for these two external TLBs have already been generated using com2java, and are included in dotnet.jar. If you wish to generate these proxies yourself, you can do so by running the makeDotnetProxies.bat file (included in dotnetExample.zip). Make sure you run this batch file before running the run.bat file.

GUI .NET Component

Please note that if your .NET component was a GUI component that you wanted to embed in a Java frame, you would also need to include the /AwtForOcxs option in the FLAGS variable so that the above commands become:

  set TYPELIBS=
  set TYPELIBS=%TYPELIBS% /ExternalTypelib c:\WINNT\system32\stdole2.tlb=stdole2
  set TYPELIBS=%TYPELIBS% /ExternalTypelib c:\WINNT\Microsoft.NET\Framework\v1.1.4322\mscorlib.tlb=mscorlib
  set FLAGS=/ClashPrefix zz_ /AwtForOcxs %TYPELIBS%
  com2javacmd %FLAGS% .\compcs\CompCS.tlb compcs .\compcs
  javac compcs\*.java

7. Compile and run the Java client

Non-GUI .NET Component

  javac CSClient.java
java -JINTEGRA_NATIVE_MODE CSClient

The above example uses native mode. If you wish to run in DCOM mode, please note that the setdllhost tool will not work on .NET-based DLLs. In order to run in DCOM mode, you must register the DLL under COM+. For more information on COM+, please see the COM+ example in the documentation.

GUI .NET Component

Please note that if your .NET component was a GUI component that you wanted to embed in a Java frame, you would need to modify the Java client as demonstrated in the example Accessing the Adobe Acrobat Reader Control from Java Swing.

Related Articles
No Related Articles Available.

Article Attachments
dotnetExample.zip

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 10/17/2006.
Last Modified by J-Integra KB Admin.
Article has been viewed 12213 times.
Rated 4 out of 10 based on 4 votes.
Print Article
Email Article