Click here to Skip to main content
13,140,231 members (50,082 online)
Click here to Skip to main content
Add your own
alternative version


75 bookmarked
Posted 26 Aug 2011

Using the Java Native Interface in C#

, 16 Feb 2016
Rate this:
Please Sign up or sign in to vote.
How to invoke Java methods from a .NET application.


The JNI or Java Native Interface is a set of features available in the Java platform. Applications that use the JNI can incorporate Java code with code written in other languages such as C++, Delphi, and C#.

In my example, I will show you how to call a couple of Java methods from a C# client.


You can reuse your existing libraries/classes that were previously written in Java from your new application written in C#. This means that you do not have to abandon your legacy code written in Java and reinvent the wheel again in C#.


One of the most important things to be familiar in JNI is Field Descriptors. As you will notice in the test harness, when I call a Java method that takes a string parameter, I pass in "Ljava/lang/String;". This represents a field type in the Java programming language. For example, if you wanted to represent an int field, then you would use "I", a float field would be "F", and a boolean field would be "Z", etc.

The table below describes the primitive types in the Java programming language and the corresponding types in JNI. All field descriptors that start with L are dealt with as objects (note: a string is passed as an object) and must be terminated by a ";" character. An array type is always prefixed with the "[" character.

JNI Field DescriptorJava Language Type

Method descriptors make use of the fields descriptors and describe the structure of a Java method. There are no spaces between the field descriptors in a method descriptor.

Apart from the void return type which is denoted by V, all other return types use the field descriptor. The table below describes the Java method declaration and the corresponding JNI descriptor. The JNI method descriptor is used when calling a Java method from C# via JNI.

Java Method DeclarationJNI Method Descriptor
String foo();"()Ljava/lang/String;"
Void bar(int I, bool b);(IZ)V

Using the code

An example of calling Java code from a .NET GUI application

Once a reference is set to the assembly JNI located in the download section of my code, you can start to call Java methods in only a few lines of code.

The first thing that needs to be done is to create a dictionary object that will contain all of the parameters to pass to the Java Virtual Machine. In the example below, I am doing the minimum of setting the class path that will tell the JVM where to look for the classes and packages.

private Dictionary<string, string> jvmParameters = new Dictionary<string, string>();
jvmParameters.Add("-Djava.class.path", Location of the java class);

Once the JVM parameters have been assigned to the dictionary object, an instance of JavaNativeInterface can be created. Once created, the method LoadJVM needs to be called with the JVM parameters, and this will then load up the Java Virtual Machine. Once loaded, the user calls the method to instantiate the Java object (note that the use of the method InstantiateJavaObject is optional as the user may just want to call a static method, in which case, this method does not need to be called; however, it will not cause any harm).

Java = new JavaNativeInterface();
Java.LoadVM(jvmParameters, false);
Java.InstantiateJavaObject(Name of the java class excluding the extension);

Once the JVM has been loaded and a class instantiated, the user may call any method they wish. First create an object list that will contain all of the parameters to pass into the Java method. As it is an object list, it can hold parameters of different types as everything inherits from an object.

List<object> olParameters = new List<object>();
olParameters.Add(Value of the parameter to be passed to the java method);

Next, simply call the generic method CallMethod passing in the return type of the Java method as the template type. In the example below, I am calling CallMethod<string> which means that the return type of my Java method that I want to call is a string.

Next, pass in the name of the Java method to call and the method descriptor (see above); finally, pass in the list of all of the parameters. (Note: If no parameters are required, then pass in an empty list.)

Java.CallMethod<string>("AddTwoNumbers", "(IILjava/lang/String;)I", olParameters);

Well, I guess that wraps it all up, but remember that there is so much more you can do with JNI. The test application was just a quick and dirty way to demonstrate the basics of the JNI component. For a full understanding of JNI, I advise you to read this book:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Simon Agholor
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionProblem with JNI Pin
SteakdeNiche14-May-14 23:38
memberSteakdeNiche14-May-14 23:38 
AnswerRe: Problem with JNI Pin
Simon Agholor15-May-14 11:28
memberSimon Agholor15-May-14 11:28 
GeneralRe: Problem with JNI Pin
SteakdeNiche15-May-14 21:09
memberSteakdeNiche15-May-14 21:09 
QuestionException java.lang.UnsupportedClassVersionError: Pin
Member 1068332924-Apr-14 8:42
memberMember 1068332924-Apr-14 8:42 
AnswerRe: Exception java.lang.UnsupportedClassVersionError: Pin
Simon Agholor24-Apr-14 10:16
memberSimon Agholor24-Apr-14 10:16 
Suggestionint long short change to Int32 Int64 Int16 Pin
Member 1047361610-Feb-14 15:17
memberMember 1047361610-Feb-14 15:17 
Questioninvoke newString pass string as byte[] instead Pin
Member 104736167-Feb-14 18:13
memberMember 104736167-Feb-14 18:13 
GeneralMy vote of 1 Pin
thefiloe16-May-13 23:47
memberthefiloe16-May-13 23:47 
AnswerRe: My vote of 1 Pin
Simon Agholor26-Nov-13 5:49
memberSimon Agholor26-Nov-13 5:49 
SuggestionSignature parsing and array parameters Pin
Member 903894719-Apr-13 2:32
memberMember 903894719-Apr-13 2:32 
GeneralRe: Signature parsing and array parameters Pin
Simon Agholor21-Apr-13 23:11
memberSimon Agholor21-Apr-13 23:11 
GeneralRe: Signature parsing and array parameters Pin
Member 903894722-Apr-13 1:07
memberMember 903894722-Apr-13 1:07 
GeneralRe: Signature parsing and array parameters Pin
Simon Agholor22-Apr-13 1:54
memberSimon Agholor22-Apr-13 1:54 
Questioncan not work on window 8 x64 Pin
yangyancheng27-Mar-13 17:27
memberyangyancheng27-Mar-13 17:27 
AnswerRe: can not work on window 8 x64 Pin
Simon Agholor27-Nov-13 1:17
memberSimon Agholor27-Nov-13 1:17 
BugFailure passing objects and arrays into Java Pin
jkrug11-Feb-13 7:58
memberjkrug11-Feb-13 7:58 
AnswerRe: Failure passing objects and arrays into Java Pin
Simon Agholor27-Nov-13 1:19
memberSimon Agholor27-Nov-13 1:19 
QuestionComplex Data Types, Methods, Structures, Constructors and Classes Using Java Native Interface in C# 2008 Pin
MrKyaw27-Jan-13 19:15
memberMrKyaw27-Jan-13 19:15 
AnswerRe: Complex Data Types, Methods, Structures, Constructors and Classes Using Java Native Interface in C# 2008 Pin
MrKyaw27-Jan-13 20:00
memberMrKyaw27-Jan-13 20:00 
GeneralRe: Complex Data Types, Methods, Structures, Constructors and Classes Using Java Native Interface in C# 2008 Pin
MrKyaw27-Jan-13 22:09
memberMrKyaw27-Jan-13 22:09 
AnswerRe: Complex Data Types, Methods, Structures, Constructors and Classes Using Java Native Interface in C# 2008 Pin
Simon Agholor27-Nov-13 1:24
memberSimon Agholor27-Nov-13 1:24 
AnswerImplementing IDisposable Pin
Simon Agholor15-Jan-13 2:32
memberSimon Agholor15-Jan-13 2:32 
BugYou leek a lot of unmanaged resources Pin
leftler26-Aug-12 10:09
memberleftler26-Aug-12 10:09 
AnswerRe: You leek a lot of unmanaged resources Pin
Simon Agholor11-Mar-13 5:38
memberSimon Agholor11-Mar-13 5:38 
QuestionFinding files in JNI Pin
andreh12313-Aug-12 15:00
memberandreh12313-Aug-12 15:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 17 Feb 2016
Article Copyright 2011 by Simon Agholor
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid