Call .NET logging package from Java

Logged Message Logged Message
Javonet Quick Start Guide

For more information how to use Javonet in common scenerios like creating objects, invoking methods, getting/setting fields, subscribing events and others please refer to our Javonet Quick Start Guide

Download Sample Project

Use the links below to download full source code and binaries of the sample project created in this tutorial:

Download Source Code and Binaries

Introduction

In this tutorial you will learn how to use any .NET library in Java. As example we will show how to call .NET logging package (log4net), from Java.

Use cases

This solution might be helpful in any situation when have to deal with any API library, external component, integration component or custom library written in .NET that must be used from Java.

Prerequisites

To build Java application using .NET logic you need Javonet developer license and Javonet desktop or server license depending on your deployment strategy. For this tutorial we will also use log4net.dll file.

Step by step instruction

1) First we will create our Java Project in Eclipse using menu “File > Java Project”. Give the name “JavonetLoggingApp” and press “Finish”.

2) When our application is ready we have to copy “javonet.jar” and “log4net.dll” to our new project. Next right click on your project go to “New > Class” and in the “New Java Class” window provide name “JavonetLoggingAppMain” and check field “public static void main(String[] args)” so the default entry method will be created.

3) Next step is to add “javonet.jar” file reference to our project. Right click on the project choose “Build Path > Configure Build Path” and in the new window go to “Add JARs” and choose “javonet.jar” from our project location. Accept all windows and close until you will get back to the project view. Our project should look like this:

Logging Project Logging Project 5) Now we can build our application. The first step is to activate Javonet and tell that we are going to use log4net.dll. Add following code to your main method:

  Javonet.activate("[email protected]", "your-javonet-license-key", JavonetFramework.v40);
  Javonet.addReference("log4net.dll");

Now we can retrieve and use any types defined in log4net.dll the same way as it would happen when we add reference to any DLL in Visual Studio .NET project. In AddReference method we can provide filename, full path or just assembly name if we want to use assembly from GAC. In this case log4net.dll is located in our Java project directory so we just provide file name.

6) Javonet initialization is done, so we can start using log4net in our Java application. Next lines of code will call “Configure” method on static “BasicConfigurator” type to initialize log4net with default console appender and next we will get new logger and log sample message:

  Javonet.getType("BasicConfigurator").invoke("Configure");

  NObject log = Javonet.getType("LogManager").invoke("GetLogger","main");
  log.invoke("Debug","Testing Javonet!");

As you see to access any type we use “Javonet.getType(string)” method, next we can invoke any static method against this type. In line 03. we get static LogManager type and call method “GetLogger” to create new instance of ILogger object. This object can be stored in universal NObject variable that works as handler for any .NET type. Next using object stored in our NObject variable we can perform any operations against that .NET instance, in this case we call method “Debug” providing as argument message “Testing Javonet!”. And this is the final result:Logged Message Logged Message Below you can check the full source code of this application:

import com.javonet.Javonet;
import com.javonet.JavonetException;
import com.javonet.JavonetFramework;
import com.javonet.api.NObject;

public class JavonetLoggingAppMain {

  public static void main(String[] args) throws JavonetException {
    Javonet.activate("[email protected]", "your-javonet-license-key", JavonetFramework.v40);
    Javonet.addReference("log4net.dll");

    Javonet.getType("BasicConfigurator").invoke("Configure");

    NObject log = Javonet.getType("LogManager").invoke("GetLogger","main");
    log.invoke("Debug","Testing Javonet!");
  }

}

To test it yourself you can download sample source code below.

Download Sample Project

Use the links below to download full source code and binaries of the sample project created in this tutorial:

Download Source Code and Binaries