Access Java from .NET

Java/J2EE .NET Interoperability Products Page

This example demonstrates how to access Java from .NET 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 various Java objects exposed as a .NET Singleton, Client Activated Object (CAO) and "marshal-by-value" object using J-Integra for .NET. This example also demonstrates how a .NET client can catch a custom exception thrown by a java 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 Java from .NET

Finally, we will extend this example to show you how to access the same Java objects using HTTP binary instead of TCP binary. Once you have completed the Steps to Follow, take a look at Access Java from .NET Using J-Integra® for .NET's internal Web server.

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 server.

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

Steps to Follow

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

  2. Compile and Run the J-Integra for .NET Server

  3. Run GenNet and Compile the Proxies

  4. Compile and Run the .NET Client

Addendum

  1. Access Java from .NET Using J-Integra for .NET's Web Server

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 as a server so it can respond to incoming TCP requests. 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. Select File - Open and open janet.xml located in $USER_INSTALL_DIR$\examples\csharp_java.

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

  4. Select java_server.Factory under Local Objects. Here you see that J-Integra® for .NET exposes this class as a Singleton at the at the given machine address and port using TCP.

    Using Janetor configuration tool setup local objects

    The default node above exposes the remaining classes with the same configuration details, except that they are not Singletons.

    Using Janetor configuration tool setup local default object

  5. Select the Exceptions tab under General->Runtime. Here you see that the java_server.CustomException class has been added to the list of Java exceptions to map to .NET.

    Using Janetor configuration tool setup runtime and exceptions

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

Compile and Run the J-Integra® for .NET Server

Perform this step on the Java platform.

In this step, you will compile the Java code and run the J-Integra for .NET server.

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

    javac -classpath . java_server\*.java
    
  2. To run the server, in the same directory enter:

    java -cp $USER_INSTALL_DIR$\lib\janet.jar;. com.intrinsyc.janet.Main
    

Run GenNet and Compile the Proxies

Perform this step on the Java platform.

In this step, you will use the GenNet tool to generate .NET proxies for the Java classes.

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

    Launch GenNet from the Start Menu.

    Command-Line

    Change directory to $USER_INSTALL_DIR$ and then enter:

    java -jar lib\gennet.jar
    
  2. The first screen of GenNet shows a list of machines that have GenService running (screenshot not shown). Select the appropriate one and click Next.

  3. The next screen prompts for the components (JAR files or directories) to be added to the CLASSPATH. Each of these components are required in order for the Java runtime to resolve references to Java classes when generating .NET proxies. You'll need to add the following component:

    - $USER_INSTALL_DIR$\examples\csharp_java

    Using GenNet generate .NET assemblies for Java Objects

    Move on to the next step by clicking Next.

  4. The next screen prompts for the specific Java classes that will have .NET proxies generated for them. Click on Browse... to add the following classes (use the CTRL key to select multiple classes):

    - java_server.ClassByValue
    - java_server.ClassByReference
    - java_server.ServerInterface
    - java_server.CustomException
    - java_server.Factory

    Make java_server.ClassByValue a "marshal-by-value" object by clicking on Options... and checking Pass by value.

    Adding Java classes to be exported to .NET in GenNet

    Move on to the next step by clicking Next.

  5. The next screen prompts you to specify the .NET Assembly name along with the output directory. The screen shot may show a different output directory than the value of Output Directory shown below, but it should be pointing to $USER_INSTALL_DIR$\examples\csharp_java\csharp_client\bin\Debug.

    Setting .NET assembly output directory in GenNet

  6. You should see a message that the proxy generation was successful. Click Exit to exit GenNet.

    GenNet generates proxies and .NET assemblies successfully

Compile and Run the .NET Client

Perform this step on the .NET platform.

In this final step, you will configure the .NET client using a configuration file, then compile and run it.

Along with .NET proxies, GenNet generates .NET configuration files to be used as templates - one for the HTTP channel and one for the TCP channel. As the .NET client will be using .NET Remoting's TCP binary to connect to the Java server, we will modify remoting_tcp.config. You can find this in the same directory that the proxies were generated in.

Note: In the .NET code, notice that a call is made to RemotingConfiguration.Configure("remoting_tcp.config");
  1. Modify remoting_tcp.config to look like:

    <configuration>
     <system.runtime.remoting>
    
        <application>      <client>
            <wellknown url="tcp://localhost:7562" type="java_server.Factory, JanetExample"/>
          </client>      <channels>        <channel ref="tcp">          <clientProviders>            <formatter ref="binary"/>          </clientProviders>        </channel>      </channels>    </application>  </system.runtime.remoting> </configuration>

    Notice that tcp://localhost:7562 matches what was entered under the java_server.Factory in Janetor. Change accordingly if necessary.

  2. Load csharp_client.sln located in $USER_INSTALL_DIR$\examples\csharp_java\csharp_client and press F5 to run, or simply run the executable 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 example, but you will not be able to open the solution file. See build.bat in $USER_INSTALL_DIR$\examples\csharp_java\csharp_client.
  3. A console should come up and you should see the output:

    Hello World!  From Singleton
    Hello World!  From Marshal-By-Ref 1
    Hello World!  From Marshal-By-Val 1
    Successfully caught custom exception: java_server sample custom exception
    Press any key to continue...
    

    Press Enter to exit.

    Examine the .NET source to look at the various tests that the client runs to access the Java objects.

You can use the same technique that was illustrated here for custom exceptions to also catch java language exceptions. For example, if the java server throws a java.lang.IllegalArgumentException, you can explicitly catch this exception in your .NET client by using GenNet to generate a proxy for the java.lang.IllegalArgumentException and using Janetor to add this exception class to the J-Integra® for .NET runtime exception map. Note that if don't generate proxies for specific custom / system exception classes, or don't include them in the J-Integra® for .NET runtime exception map, then by default the .NET client will receive a System.Runtime.Remoting.RemotingException.

Access Java from .NET Using J-Integra® for .NET's Web Server

The previous example showed how to access Java objects by starting the J-Integra® for .NET server in TCP binary mode. The following addendum will show you how to harness J-Integra® for .NET's internal Web server to access the same Java objects in HTTP binary mode. You must first successfully deploy the example in TCP binary (as instructed above) before proceeding further.

Steps to Follow

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

  2. Run the J-Integra® for .NET Server

  3. Configure and Run the .NET Client

Run Janetor to Configure J-Integra® for .NET

Perform this step on the Java platform.

  1. Launch Janetor again, and open janet_http.xml located in $USER_INSTALL_DIR$\csharp_java.

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

  3. Select Web Server under General. Here you see that Use Internal Web Server is checked and the internal Web server configuration details (for a full description see the Janetor documentation).

    Turn on J-Integra Internal Web Server in Janetor

  4. Select default under Local Objects. Here you see that the protocol has been changed from the previous scenario to HTTP and other fields are disabled. This is because the internal Web server is now enabled and the disabled fields must now be changed under Web Server.

    Using Janetor setup default local object using HTTP protocol

  5. Select java_server.Factory under Local Objects. Here you see that the protocol has been changed from the previous scenario to HTTP and other fields are disabled. This is because the internal Web Server is now enabled and the disabled fields must now be changed under Web Server.

    Using Janetor setup local objects using HTTP protocol

  6. If you select the Exceptions tab under General->Runtime, you will notice that the java_server.CustomException class has been added to the list of Java exceptions to map to .NET, as in the TCP case.

  7. Before you exit Janetor, save the configuration by going to the File menu item and clicking Save.

Run the J-Integra® for .NET Server

Perform this step on the Java platform.

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

    java -cp $USER_INSTALL_DIR$\lib\janet.jar;$USER_INSTALL_DIR$\lib\org.mortbay.jetty.jar;$USER_INSTALL_DIR$\lib\javax.servlet.jar;. -DJANET_CONFIG_FILE=janet_http.xml com.intrinsyc.janet.Main
    

    The extra components in the CLASSPATH are the libraries required to start the internal Web Server.

Configure and Run the .NET Client

Perform this step on the .NET platform.

  1. In $USER_INSTALL_DIR$\examples\csharp_java\csharp_client\bin\Debug, modify remoting_http.config to look like:

    <configuration>
    
      <system.runtime.remoting>    <application>      <client>        <wellknown url="http://localhost:8080/janetexample/java_server.Factory.soap" type="java_server.Factory, JanetExample" />      </client>      <channels>        <channel ref="http">        <clientProviders>          <formatter ref="binary"/>        </clientProviders>        </channel>      </channels>    </application>  </system.runtime.remoting> </configuration>

    Notice that http://localhost:8080/janetexample matches what was entered under java_server.Factory in Janetor. Change accordingly if necessary.

  2. Load csharp_client.sln located in $USER_INSTALL_DIR$\examples\csharp_java\csharp_client and change the first line of code from:

    	RemotingConfiguration.Configure("remoting_tcp.config");
    

    to:

    	RemotingConfiguration.Configure("remoting_http.config");
    
  3. Press F5 to run, or simply run the executable 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 example, but you will not be able to open the solution file. See build.bat in $USER_INSTALL_DIR$\examples\csharp_java\csharp_client.
  4. You should expect to see the exact same output as described in the previous scenario, except now the example is using an HTTP channel provided by the internal Web Server.

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