Calling Java Classes Directly from .NET
Wayne Citrin
t.
Build a Logging Example
Most software systems of any size or complexity contain a logging facility. Good logging packages exist for both Java (log4J) and .NET (log4N, which is the .NET equivalent of log4J). However, if a system contains both .NET and Java components, each side would be logged into its own logging package, which would generate two log files and make it difficult to collate or correlate the events logged. If you could instead use a single logging package to log to a single log file, you could easily see the sequence in which .NET-originated and Java-originated events took place. A single logging package also simplifies handling timing and file access conflicts among the multiple processes and threads attempting to log events.
You could choose either a .NET-based or Java-based logging package as the single logging package to be used from both .NET and Java. This example uses the Java-based package log4J to do the logging, but accesses the log from both sides, to demonstrate how transparently .NET can use existing Java classes.
The goal is to create a simple application containing both Java and .NET classes, where both the Java and .NET classes use the Java-based log4J logging package. The application has a .NET-based Windows Forms GUI, which calls the Java classes that perform the logging operations.
In the remainder of this article, you’ll walk through the process of setting up the interoperability project, creating the proxies, and building and running the project.
Requirements
To complete the project, you’ll need Microsoft Visual Studio .NET 2003 and the .NET Framework 1.1 installed on your machine. You can get the project to build using the .NET Framework SDK or Visual Studio .NET 2002, and .NET Framework 1.0, but you will need to make some modifications outside the scope of this article. The JNBridgePro documentation contains some suggestions on how you can proceed with older versions of .NET.
On the Java side, you must have a Java Development Kit (JDK) installed on your machine. You may use any JDK of version 1.2.2 or later. You can download a free JDK from Sun’s Java site.
Setting up
To set up the project, download the zip file containing the project code and unpack it. The zip file contains two sub-folders, Project and Completed. The Project folder contains all the code needed to create the project. The Completed folder contains the completed, ready-to-run, project, in case you don’t want to create the project yourself.
The Project folder contains dotNet and Java folders containing the .NET and Java portions of the project code, respectively. The Java folder contains a single Java class named JavaClass, which exposes a recordEvent() method that logs a supplied message to a specific logging category, as shown below:
package devX.logging; import org.apache.log4j.*; public class JavaClass { private static Category cat = Category.getInstance(“JavaClass”); public void recordEvent(String message) { cat.debug(message); } }
Although you’ll find the source file in the accompanying code, JavaClass is already compiled, so you need do nothing else to it.
In addition to Java code, the Project folder contains two .NET C# source code files. Form1.cs defines a Windows application that calls both .NET and Java objects, each of which logs events through the log4j Java-based logging package. The DotNetClass.cs file defines a .NET class similar to the JavaClass mentioned above. The DotNetClass also contains a recordEvent() method that logs a supplied message to a specific logging category:
using System; using org.apache.log4j; namespace LogDemo { /// <summary> /// Sample .NET class that calls log4j. /// </summary> public class DotNetClass { private static Category cat = Category.getInstance("DotNetClass"); public DotNetClass() { } public void recordEvent(String message) { cat.debug(new java.lang.JavaString(message)) } } }
Category is a .NET class that acts as a proxy for the corresponding Java Category class in the log4j package. .NET code accesses Java classes and objects by interacting with the corresponding proxy object. You’ll see more about proxies and how they’re generated and used in the remainder of this article.
The Completed folder contains everything in the Project folder plus additional files that allow the project to run without additional changes.
In addition to the project code, download the logging package log4j and unpack it in a location of your choice.
Finally, you’ll need JNBridgePro from the JNBridge Web site. For the purposes of this project, you may download either the EE or SE version. JNBridge provides 15 days of free use from the time that you install JNBridgePro; which should be sufficient to complete this project.
Install JNBridgePro by double-clicking on the downloaded installation file and following the instructions. Make sure that JNBridgePro is set up correctly by launching the JNBProxy proxy generation tool (through the Start menu or by double-clicking on the desktop icon). The first time you do this, you’ll see a dialog box that allows you to specify the Java configuration (see Figure 1). Make sure to check the “Start Java automatically” checkbox and specify a valid path is specified in the “Locate the java.exe file …” text box. After doing so, click the OK button. You can close JNBProxy at this point, or you can leave it open and proceed to the next step.
Generating.
Setting Up the .NET Program
Double-click on the Project\DotNet\LogDemo.sln file to open the .NET project. Before building the project, add the proxies to the project by adding a reference to the assembly loggingProxies.dll that you created in the preceding section. You also need a reference to the assembly jnbshare.dll that you’ll find in the JNBridgePro installation folder. Jnbshare.dll contains the core JNBridgePro functionality used to manage communications with the Java classes. After adding the references, you should be able to build the project without errors.
To finish setting up the .NET program, copy the file jnbproxy.config from Project\DotNet into your build folder, which will either be bin\release or bin\debug, depending on your Visual Studio project build settings. jnbproxy.config contains configuration information that tells the .NET side where the Java code is located and which protocol will be used to communicate with it. In this case, jnbproxy.config specifies that the Java code is located on the same machine as the .NET code, that it will listen on port 8085, and will communicate with the binary protocol.
Running the Program
If you examine Form1.cs, you’ll see that it uses the BasicConfigurator class, which for demonstration purposes causes logged messages to appear in the Java console window. Note that it’s quite possible to log to files, JMS publishers, or other endpoints.
Before starting the .NET program, you’ll need to start the JVM containing the Java classes, so they’ll be able to receive the requests from the .NET side. Do this by opening a command-line window and typing in the following command line:
java ->
To be safe, surround each of the paths in the preceding command line with quotes, because they may contain spaces. The jnbcore.jar file contains the core JNBridgePro functionality that allows Java classes to communicate with the .NET side. The jnbcore_tcp.properties file contains configuration information that tells Java which communications protocol to use and which port to listen on for requests. The sample project uses the binary protocol and listens on port 8085. Figure 3 shows what you will see if all goes well.
Next, start the .NET program from Visual Studio .NET, or simply by double-clicking on LogDemo.exe. Figure 4 shows the running application.
Clicking on the “Log .NET event” button causes the DotNetClass to log an event from .NET. Click on the “Log Java event” button to cause JavaClass to log an event from Java. You can either supply a custom message to log or just use the supplied defaults. In each case, the Java-based log4j package performs the logging operation, and displays the logged messages in the console window, as shown in Figure 5.
In this project you’ve seen how to log messages from both .NET and Java code into a common logging package—in this case running on the Java platform. Using a common logging package consolidates .NET-originated and Java-originated logging messages in the same console window or file, and lets you easily see the sequence of from both the .NET and Java sides.
More generally, you’ve seen how call to Java classes directly from .NET by using a runtime bridge. In the sample application, .NET code calls Java classes in the log4j package, and also calls the Java class JavaClass. You can easily extend this technique of directly accessing Java classes from .NET to other Java class libraries, and you can also use it to let .NET classes access EJBs, JMS queues, or other Java and J2EE capabilities. Using a runtime bridge is simple, transparent, and extensible, because your .NET code accesses the Java code through proxies, and has no idea that it’s talking to Java classes.
Posted by Nakshtra Choudhary on March 9, 2012 at 4:49 pm
hummmm, is it possible ?? that we are calling a java class from .net program ?
Posted by Denno Secqtinstien on March 9, 2012 at 4:50 pm
yes it is possible in .net ?
Posted by Destiny Nellis on March 10, 2012 at 11:25 am
You are my inspiration , I possess few blogs and infrequently run out from to post .
Posted by Kurt Melady on March 18, 2012 at 1:17 antivirus software alert on March 23, 2012 at 10:33 am
Regularly have a look at your current personal computer’s total program. I prefer to operate reads immediately since they decrease your personal computer and may consider some time. If perhaps section of your personal machine method definitely seems to be contaminated, you’ll be able to check out the particular believe portion merely. If you’re by using a free plan, make sure that that removes the actual adware and spyware it’s got discovered. Many of these free of charge packages may identify the particular dangers but ask you to purchase the put in get correctly to eliminate the infection. I think these specific packages should be deleted as well as swapped out by simply programs which have entire functionality.
Posted by south bronx on April 11, 2012 at 8:05 am
I’m just writing to make you understand what a remarkable experience my princess found reading through the blog. She picked up too many details, which included what it’s like to have an excellent helping spirit to let the others really easily gain knowledge of certain extremely tough topics. You truly surpassed my expectations. Thank you for providing such priceless, dependable, explanatory and in addition unique thoughts on this topic to Emily.
Posted by Cordell Elway on April 11, 2012 at 8:50 am
My brother recommended I might like this website. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!
Posted by Merideth Sasala on April 13, 2012 at 11:11 noopept on April 17, 2012 at 10:26 pm
Very interesting information!Perfect just what I was looking for!
Posted by Barton Galka on April 18, 2012 at 1:45 am
Wow, incredible blog format! How long have you ever been blogging for? you made running a blog glance easy. The total glance of your website is magnificent, let alone the content!
Posted by Vitamin D on April 18, 2012 at 2:32 am
Greeting from over the world. informative article I must return for more.
Posted by bionaire steam mops on April 21, 2012 at 4:29 pm
I every time download a full show in parts, that’s always existing at YouTube, for the reason that my network connection is very slow and YouTube fulfils my desires.
Posted by Top Web Hosting on April 24, 2012 at 11:56 am
Web hosting getting cheaper with offering good functionalities.I have tried some Awesome web hosting providers and found them great
Posted by Winford Rownd on April 25, 2012 at 11:19 am
Hi my loved one! I want to say that this article is awesome, nice written and come with approximately all significant infos. I would like to look extra posts like this .
Posted by Allan Hanley on May 3, 2012 at 7:25 am Tarah Ying on May 10, 2012 at 12:05 pm
Was wondering if there was any wordpress photo portfolio similar to links below.
Posted by millitary command centers on May 15, 2012 at 10:20 am
Good report, but noooooo not OsCommerce! I dose on tylenol when using OSCommerce so I can avoid the migranes. Check out Interspire, it’s what I have been using lately and it’s like a lighter edition of magento.
Posted by official link on May 15, 2012 at 11:55 pm
I believe one of your advertisements caused my web browser to resize, you may well want to put that on your blacklist.
Posted by fast personal loans discussions on May 16, 2012 at 12:14 am
Please message me with any pointers on how you made your website look this awesome, I would be thankful!
Posted by squash camps discussion on May 16, 2012 at 5:55 am
Jesus Christ there is a lot of spammy comments on this site. Have you ever before believed about attempting to get rid of them or installing a tool?
Posted by improve your memory on May 16, 2012 at 6:54 am
An interesting blog post there mate ! Thanks for the post !
Posted by amusement parks in new york city website on May 16, 2012 at 7:18 am
As soon as I originally commented I clicked on the Notify me when new comments are added checkbox and currently each and every time a remark is added I receive four emails with the identical comment.
Posted by continue reading on May 16, 2012 at 6:35 pm
Even though I really like this publish, I believe there was an spelling error shut towards the finish of the 3rd section.
Posted by Julian Renart on May 17, 2012 at 10:54 am
I’ve been browsing on-line greater than 3 hours lately, but I by no means discovered any attention-grabbing article like yours. It¡¦s pretty value sufficient for me. In my opinion, if all website owners and bloggers made good content material as you probably did, the web will likely be much more helpful than ever before.
Posted by purchase here on May 17, 2012 at 6:37 pm
Amazing article, thanks, I will subscribe to you RSS later!
Posted by hidden safes for the home on May 18, 2012 at 1:00 am
Weird , this post shows up with a dark color to it, what shade is the primary color on your web-site?
Posted by advertiser on May 18, 2012 at 1:38 am
Re: Whoever made the remark that this was a great internet site truly needs to possess their head evaluated.
Posted by gifts idea on May 18, 2012 at 2:46 am
Hey there, I just hopped over to your web-site thru StumbleUpon. Not somthing I would typically browse, but I appreciated your views none the less. Thanks for creating something worthy of reading.
Posted by cliffordwilliamrice.com on May 18, 2012 at 8:32 am
Just discovered this site through Google, what a pleasant shock!
Posted by on May 22, 2012 at 8:59 am
Do youve a spam concern on this web site; I also am a blogger, and I was questioning your scenario; we have made some good techniques and we are looking to exchange options with other people, be certain to shoot me an e-mail if interested.