Access .NET from Java

Java/J2EE .NET Interoperability Products Page

This example demonstrates how to access .NET from Java client. J-Integra® for .NET is a Java interoperability component that bridges Java/J2EE and Microsoft .NET. It provides bi-directional access of Java objects and .NET components.

Synopsis

Use this example as a guideline to show you how to access a .NET Singleton, Client Activated Objects (CAO) and "marshal-by-value" objects from Java using J-Integra for .NET. This example also demonstrates how a java client can catch a custom exception thrown by a .NET server.

The Steps to Follow will show you how to deploy J-Integra® for .NET correctly for this example. Refer to the diagram below for an overview of the scenario:

Access .NET from Java

Note: $USER_INSTALL_DIR$ is the directory where J-Integra for .NET has been installed. If this location contains whitespace characters (e.g. C:\Program Files\...), you may need to include it within "" when referencing it at the command-line.

Prerequisites

  1. A platform with Microsoft Visual Studio .NET 2003 (7.1.3088 or higher) installed, or at least the .NET Framework 1.0.3705 or higher. This will be known throughout the example as the .NET platform.

  2. A platform with J-Integra for .NET 1.5.1295 (or higher) installed. This will be known throughout the example as the Java platform.

  3. A version of JDK (1.3.1 or higher) installed on the Java platform, and the bin directory included in the path. This required to compile and run the Java client.

  4. Verify that GenService is installed correctly on the .NET platform.

Steps to Follow

  1. Compile the .NET Assembly

  2. Run GenJava and Compile the Proxies

  3. Configure and Run the .NET Server

  4. Run Janetor to Configure J-Integra for .NET

  5. Compile and Run the Java Client

Compile the .NET Assembly

Perform this step on the .NET platform.

In this step, you will need to compile the .NET code.

  1. Load csharp_server.sln located in $USER_INSTALL_DIR$\examples\java_csharp\csharp_server with Visual Studio .NET 2003. Press Ctrl + Shift + B to build. If successful, this should build csharp_server.exe in the bin\Debug directory.

    Note: If you happen to have an older version of Visual Studio .NET, it is still possible to compile the code, but you will not be able to open the solution file. See build.bat in $USER_INSTALL_DIR$\examples\java_csharp\csharp_server.

Run GenJava and Compile the Proxies

Perform this step on the Java platform.

In this step, you will use the GenJava tool to generate Java proxies from the .NET Assembly.

Note: GenJava saves configuration details to an XML file. For your convenience, the settings described below have already been saved to the file genjava.xml in the directory $USER_INSTALL_DIR$\examples\java_csharp\java_client. If you'd prefer not to manually enter the settings as instructed below, simply copy this file over to $USER_INSTALL_DIR$ before starting GenJava.
  1. Windows

    Launch GenJava from the Start Menu.

    Command-Line

    Change directory to $USER_INSTALL_DIR$ and then enter:

    java -jar lib\genjava.jar
    
  2. The first screen of GenJava prompts for the location of the .NET assembly along with the output directory where the Java proxies will go. They should be (respectively):

    - $USER_INSTALL_DIR$\examples\java_csharp\csharp_server\bin\Debug\csharp_server.exe
    - $USER_INSTALL_DIR$\examples\java_csharp\java_client

    Setup source assembly name and output directory for Java proxy in GenJava

    Move on to the next step by clicking Next.

  3. You should see a message that the proxy generation was successful. Click Finish to exit GenJava.

    GenJava Java Proxies for Remote Objects generation successful

  4. To compile the Java proxies, at the command-line, change directory to $USER_INSTALL_DIR$\examples\java_csharp\java_client and enter:

    javac -classpath $USER_INSTALL_DIR$\lib\janet.jar;. csharp_server\*.java
    

Configure and Run the .NET Server

Perform this step on the .NET platform.

In this step, you will need to configure and start the .NET server so it can respond to incoming TCP requests.

Along with Java proxies, GenJava generates .NET configuration files to be used as templates - one for the HTTP channel (web.config) and one for the TCP channel (remoting_tcp.config). As we will be using TCP binary, we will use remoting_tcp.config. You can find this in the same directory that the proxies were generated in.

  1. Modify remoting_tcp.config to look like:

    <configuration>
     <system.runtime.remoting>
     <application>
       <service>
         <wellknown type="csharp_server.Factory, csharp_server" objectUri="csharp_server.Factory.soap" mode="Singleton"/>
         <activated type="csharp_server.ClassByReference, csharp_server"/>
       </service>
       <channels>
         <channel port="7561" ref="tcp">
           <serverProviders>
             <formatter ref="binary" typeFilterLevel="Full"/>
           </serverProviders>
         </channel>
       </channels>
     </application>
    <!-- *** IMPORTANT - Use of the customErrors element is specific to .NET 1.1 or later. *** -->
    <!-- *** Remove the customErrors element if running under .NET 1.0 *** -->
     <customErrors mode="off"/>
     </system.runtime.remoting>
    </configuration>
    
    Note: If you are using a version of .NET Framework lower than 1.1 (1.0.3705), you will need to remove the <channel> tag (and everything in between it) and instead replace with <channel port="7561" ref="tcp"/>. .NET Framework 1.1 introduced new secure serialization - details can be found here.

    Note also: The <customErrors mode="off"/> element is required in order to configure .NET Remoting to return complete exception information to both local and remote callers. Otherwise, by default it will only return complete custom exception information to callers running on the same host - details can be found here. Use of the customErrors element is specific to .NET 1.1 or later. Remove or comment out the customErrors element if you are running under .NET 1.0.

  2. Copy remoting_tcp.config to the bin\Debug directory (where csharp_server.exe should be).

  3. Launch csharp_server.exe and you should see the console output:

    Host is ready to process remote messages.
    Press ENTER to exit
    

Run Janetor to Configure J-Integra® for .NET

Perform this step on the Java platform.

In this step, you will need to configure J-Integra for .NET to act as a client to access the .NET objects. This is done through the Janetor tool.

  1. Windows

    Launch Janetor from the Start Menu.

    Command-Line
    java -jar $USER_INSTALL_DIR$\lib\janetor.jar
    
  2. Go to File - Open and open janet.xml located in $USER_INSTALL_DIR$\java_csharp\java_client.

  3. Install your J-Integra for .NET product license. See Installing the J-Integra® for .NET license for further information.

  4. Select default under Remote Objects. Here you see that J-Integra for .NET is configured as a client to access a Singleton at the given URI and Assembly name, using TCP binary. The node below configures access to the CAO with the same configuration details.

    Setup remote objects in Janetor configuration tool

  5. Before you exit Janetor, save the configuration by going to File - Save.

Compile and Run the Java Client

Perform this step on the Java platform.

In this final step, you will compile and run the Java client.

  1. At the command-line, change directory to $USER_INSTALL_DIR$\examples\java_csharp\java_client and enter:

    javac -classpath $USER_INSTALL_DIR$\lib\janet.jar;. Client.java
    
  2. To run the client, in the same directory enter:

    java -cp $USER_INSTALL_DIR$\lib\janet.jar;. Client
    
  3. You should see the following output:

    *** Testing singleton ***
     Hello World from the Factory
    *** SUCCESS ***
    *** Testing CAO ***
     Original CAO:
       Hello, cbr1
     Modified CAO:
       Hello, cbr1 [modified]
     Another CAO:
       Hello, cbr2
     Original CAO array:
       Hello, cbr0
       Hello, cbr1
       Hello, cbr2
       Hello, cbr3
       Hello, cbr4
       Hello, cbr5
       Hello, cbr6
       Hello, cbr7
       Hello, cbr8
       Hello, cbr9
     Modified CAO array:
       Hello, [modified] cbr0
       Hello, [modified] cbr1
       Hello, [modified] cbr2
       Hello, [modified] cbr3
       Hello, [modified] cbr4
       Hello, [modified] cbr5
       Hello, [modified] cbr6
       Hello, [modified] cbr7
       Hello, [modified] cbr8
       Hello, [modified] cbr9
    *** SUCCESS ***
    *** Testing pass by value ***
    csharp_server.ClassByValue has contents: cbv1, 77, and c
    *** SUCCESS ***
    *** Testing custom exception ***
    *** SUCCESS - caught Custom exception: charp_server sample custom exception
    

    Examine Client.java to look at the various tests that the client runs to access the .NET objects.

© 2007 Intrinsyc Software International, Inc. All rights reserved. Legal