I have a simple C# class library, I wrapped it in a C++/CLI class library and loaded it into a simple Java console application. But when I run the Java application, I get the following error:
Process is terminated due to StackOverflowException.
Process finished with exit code -1073741571 (0xC00000FD)
My C# library has a Class1.cs file:
using System;
namespace CSharpClassLibrary1
{
public class Class1
{
public Class1() { }
public void Print()
{
Console.WriteLine("Hello World From C#!");
}
}
}
My C++/CLI wrapper has two files: HelloWorld.h and CppClassLibrary1.cpp.
HelloWorld.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv*, jobject);
#ifdef __cplusplus
}
#endif
#endif
I got it by running the command of the following form in my Java application: javac -h <directory> -d <directory> <source files>
.
CppClassLibrary1.cpp:
#include "pch.h"
#include "HelloWorld.h"
using namespace CSharpClassLibrary1;
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv* env, jobject obj)
{
Class1^ cls = gcnew Class1();
cls->Print();
}
I added the CSharpClassLibrary1 as reference to the C++/CLI wrapper.
I added to the "Include directories" setting the following paths:
C:\Users\ns16\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\192.6817.14\jbr\include
C:\Users\ns16\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\192.6817.14\jbr\include\win32
My Java application has a HelloWorld.java file:
public class HelloWorld {
public native void print();
static {
try {
System.loadLibrary("CppClassLibrary1");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
}
}
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
hw.print();
}
}
I specified the CppClassLibrary1 library path in the java.library.path variable.
Please help me! What am I doing wrong?
Update. A study of the problem showed the following:
In the Java application error appears in the hw.print();
line. If you comment it out, the application will start successfully.
If in the C++/CLI wrapper in the CppClassLibrary1.cpp file you replace Java_HelloWorld_print
function body to the std::cout << "Hello World From C++/CLI!";
line, the Java application will start successfully and print the Hello World From C++/CLI!
string.
If you create C# console application, add to it the C# library as references and add into Main
method the Class1 cls = new Class1(); cls.print();
code, the application will start successfully and print the Hello World From C#!
string.
I copied the C# library assembly in the directory with the java.exe file (in my system it located in C:\Users\ns16\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\192.6817.14\jbr\bin
) and now the Java application works! I would not want to move the assembly out of the project directory, but this is the only solution I found.