How to call C# function from java


I need to call C# function from java and to this I have created the following. I have a create a java header file Authenticator.h , here is the code:

#include <jni.h>
/* Header for class Authenticator */

#ifndef _Included_Authenticator
#define _Included_Authenticator
#ifdef __cplusplus
extern "C" {
 * Class:     Authenticator
 * Method:    authenticate
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
JNIEXPORT jboolean JNICALL Java_Authenticator_authenticate
  (JNIEnv *, jobject, jstring, jstring);

#ifdef __cplusplus

I have then create a C# function that Authenticate

namespace SharpAuthenticator
    public class Authenticator

        public  bool Authenticate(String username,String password)
            return username == "user" && password == "login";


Then I am trying to call the C# function from C++(project to create a dll) using the code below;

String^ toString(const char *str)
    int len = (int)strlen(str);
    array<unsigned char>^ a = gcnew array<unsigned char>(len);
    int i = 0;
    while (i < len)
        a[i] = str[i];
    return Encoding::UTF8->GetString(a);
bool authenticate(const char *username, const char *password)
     SharpAuthenticator::Authenticator::Authenticate(toString(username), toString(password));

JNIEXPORT jboolean JNICALL Java_Authenticator_authenticate
(JNIEnv *env, jobject c, jstring name, jstring pass)
    jboolean result;

    jboolean isCopyUsername;
    const char * username = env->GetStringUTFChars(name, &isCopyUsername);
    jboolean isCopypassword;
    const char * password = env->GetStringUTFChars(pass, &isCopypassword);

    result = authenticate(username, password);
    env->ReleaseStringUTFChars(name, username);
    env->ReleaseStringUTFChars(pass, password);
    return result;

And finnally create a dll that i need to call from java. The dll is created and I load it well in java but I get this error log in java. What could I be Missing.

# A fatal error has been detected by the Java Runtime Environment:
#  Internal Error (0xe0434352), pid=9708, tid=7756
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) Client VM (21.0-b17 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [KERNELBASE.dll+0x812f]
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
asked on Stack Overflow Nov 18, 2015 by MorganM

1 Answer


First of all lets create a C# file like this:

using System;
public class Test{
  public Test(){}
  public String ping(){
    return "C# is here.";

Then compile this with command below:

csc.exe /target:module Test.cs  

You can find csc.exe in install path of .NET framework. After that create java file:

public class Test{
  public native String ping();
  public static void main(String[] args){
    System.out.println("Java is running.");
    Test t = new Test();
    System.out.println("Trying to catch C# " +;

javac This generates a Test.class.

javah -jni Test This generates a Test.h file which will be included in C++ code.

After that we need to create our C++ file:

#include "stdafx.h"
#include "JAVA/Test.h"
#include "MCPP/Test.h"
#pragma once
#using <mscorlib.dll>
#using "Test.netmodule"
JNIEXPORT jstring JNICALL Java_Test_ping(JNIEnv *env, jobject obj){
  Test^ t = gcnew Test();
  String^ ping = t->ping();
  char* str = static_cast<char*>((System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ping)).ToPointer());

  char cap[128];
  strcpy_s(cap, str);

  return env->NewStringUTF(cap);


c:\>java Test

I hope this helps you. A basic example to use function C# in Java.


answered on Stack Overflow Nov 18, 2015 by phantom • edited Aug 11, 2017 by Adrian

User contributions licensed under CC BY-SA 3.0