.NET Developers Can Directly Call Any Java Package

Tags

It has already been a few weeks since the release of Javonet 1.5 which provides the ability to reference and use any Java JAR package directly from .NET applications. Now .NET developers building desktop, web, console or service application can easily and instantly reference and utilize any Java components.

Number of the companies evaluating new Javonet (working also the opposite direction) for calling Java methods from .NET is growing and we get more and more feedback with valuable suggestions.

Read further to learn how easily you can use Java in .NET.

Why Javonet?

Javonet is not a counterpart or alternative to Web Services. Native integration offered by our technology has been developed to solve all the cases that can’t be accomplished with Web Services based on XML or REST. These are all the scenarios where performance is a key factor.

Because Javonet is not a client-server solution and does not imply any serialization and deserialization to unified protocols like XML or REST as well as waives the need for any HTTP or TCP/IP communication stack, the transition of your calls between the two technologies (Java and .NET) has a minimal impact on the performance of your solution or application. Javonet overhead for each call between .NET and Java is around 10-20 millionth of a second allowing for up to 250 000 calls per second. Executing the same Java method from .NET via Web Services would take around 800 seconds 10 minutes. Therefore, Javonet is 1000x faster than web services technology.

“Javonet is 1000x faster than web services”

Javonet is in-process communication tool which means that both your CLR and JVM will run within the same OS process making it extremely secure, durable and efficient. You might be sure that there will be no need to setup and control your communication channel, manage authorization and authentication or risk of losing the connection. With Javonet your .NET code is seamlessly using Java methods like they were native part of your application.

With Javonet you can use Java methods as they are calling them with the same signature passing arguments and retrieving results. You can also easily initialize Java objects, use static objects, access fields and properties, work with collections, subscribe events and do all the rest possible operations you might need in your project. Javonet will automatically convert .NET value types into Java counterparts in both directions. You do not need to care about the way how the communication will be done, all you care about is to write your code and use your modules.

Finally, Javonet is a single file solution which means that to use Javonet , all you would need is adding a reference to single DLL file and magically you will immediately have an access to any JAR file. It makes it very convenient for unit testing, integrations testing, building on build agents and distributing with your application.

When to Use Java from .NET?

Considering the Javonet characteristics mentioned in pervious chapter we recommend using Javonet in three major scenarios:

  • Accessing Java back-end components from .NET: whenever you need to call critical module written with Java, computational, machine learning or artificial intelligence algorithm, speech recognition algorithms or just access library to your back-end or data layers, Javonet will be the perfect fit. Its incomparable performance and seamless integration within your application process make it fluent to use those modules as if they were available in .NET.
  • Access Java drivers, SDKs or JDBC components: key benefit comes from the ability to access wide range of community and open source drivers, SDKs and JDBC components written with Java. Now you do not need any wrappers, .NET implementations or alternative solution to use any system that provides great and well optimized Java components. You might call them directly from .NET with no loss in performance.
  • Accessing physical devices: many physical devices come with client libraries written with Java, it’s hard to imagine controlling multi-axis robot or healthcare machine with real-time data points processing via web services, therefore using Javonet seems to be the best choice.

 

Other: constantly we are being astonished by great solutions that our customers created with Javonet and we look forward to all other use cases that you will find suitable for our technology!

Sample Code

Enough of theory let’s see in practice how easily you can use Java packages from .NET with Javonet technology. First you can use our great fluent, chain-based API that worked great for Java developers over last years. Code below shows the example how Java method can be called from .NET.

The Java class we are going to use:

public class MainCl {
  public static Integer staticInt=4;
  
  public static String 
       Concatenate(String a, String b)
  {
     return a+b;
  }
  
  public Integer 
            Sum(Integer a, Integer b)
  {
     return a+b;
  }
  
  public String GetString()
  {
     return "Test";
  }
}

.NET code using that Java object through Javonet:

Javonet
     .AddReference(@"SampleLib.jar");
    
//Call static method
var appType = JavonetBridge
        .Javonet
        .GetType("MainCl");
String res = appType.Invoke(
     "Concatenate", "str1", "str2");
Console.WriteLine(res);

//Set Instance field
appType.Set("staticInt", 6);

//Get instance field
int res1 = appType.Get("staticInt");
Console.WriteLine(res1);

As you can see in the sample above, first you can easily add reference to any JAR package. Once this is done any object defined in that package is accessible through Javonet API. The next lines are getting the MainCl type and using its static method and instance field.

Important here is the fact that you can easily pass .NET string or integer as argument and that it will be properly passed to Java side. It is also possible to pass any reference objects.

In the next snippet you can see how easily instance of Java object can be created and called from .NET:

.NET code creating instance of Java method and calling instance method:

//Creating instance of Java class
var sampleCl = Javonet
         .New("SampleJavaClass");

//Calling instance methods
String res = sampleCl.Invoke(
             "SayHello", "Student");
Console
    .WriteLine("Java returned: "+res);

More Resources

Want more information? Or maybe you would like to jump start your first project using Java packages from .NET? Feel free to browse further resources using the links below:

.NET developers guides:
https://www.javonet.com/dotnet-devs/guides/

Samples showing how to call Java from .NET:
https://www.javonet.com/dotnet-devs/samples/

Register for Javonet free trial and download Javonet for .NET developers:
http://my.javonet.com/signup/?type=free

We work constantly to add more materials so follow us on LinkedIn, Facebook and Twitter to stay updated with latest news and more materials coming soon!