Calling Java Code in C# using IKVM results in System.TypeInitializationException error

1

I am currently trying to call Java Code in C#. One possibility is IKVM, whereupon I looked at a tutorial for this tool. I have to say, and that's really curious: the tool seems to work in part.

But now to my problem:

So I took the following tutorial (https://www.codeproject.com/Articles/594632/IKVM-NET-in-Details). Following this example, I wrote my own Java code. In addition, I have added a few more methods to the java file. My source code for testing is relatively short:

The Java source code:

package TestProject;

public class TestClassJava {

    public static void Print() {
        System.out.println("Hi C# from JAVA");
    }

    public static void PrintStr(String str) {
        System.out.println(str);
    }

    public static String returnString() {
        return "Hi C# from Java method";
    }

    public static String returnInputString(String input) {
        return input;
    }

    public static int retInt() {
        return 42;
    }

    public static int returnIntNumber(int inp) {
        return inp;
    }

    public static boolean returnTrueBoolean() {
        return true;
    }
}

The C# source code:

using System;
using System.IO;
using TestProject;
using ikvm.io;
using ikvm.lang;
using ikvm;
using ikvm.runtime;
using ikvm.extensions;

namespace IKVM_Test_Case_08_08_2019
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(TestClassJava.retInt());          // shows: 42 (works!)
            Console.WriteLine(TestClassJava.returnString());    // shows: Hi C# from Java method (works!)     

            TestClassJava.Print();                              // here appears the error System.TypeInitializationException
            TestClassJava.PrintStr("Hallo");                    // here appears the error System.TypeInitializationException  
            Console.WriteLine(TestClassJava.Print());           // can not convert from void to bool
        }
    }
}

The whimsical part now happens while running the program in C#. I try the methods in C# via Console.WriteLine(TestClassJava.retInt()); then, for example, the number 42 will be given to me, as it should be. I can also call the method returnString().

In the methods without return value, however, Print() & PrintStr(String str), I always get the following error message:

Error message:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'java.lang.StdIO' threw an exception.
  Source=IKVM.OpenJDK.Core
  StackTrace:
   at java.lang.System.get_out()
   at TestProject.TestClassJava.Print()
   at IKVM_Test_Case_08_08_2019.Program.Main(String[] args) in C:\Users\...\source\repos\IKVM_Test_Case_08_08_2019\IKVM_Test_Case_08_08_2019\Program.cs:line 19

Inner Exception 1:
MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.

I can not quite explain that, so I asked this question in the hope, that someone knows an advice.

According to the quoted tutorial, it all had to work that way. Nevertheless, I get this error message.

I hope my question is so far understandable.

java
c#
ikvm
asked on Stack Overflow Aug 11, 2019 by P_Gate • edited Aug 14, 2019 by P_Gate

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0