ikvm
ikvm copied to clipboard
IKVM NativeAOT support
Description
I'm trying to make a HelloWorld console app by using IKVM and Native AOT. The main method is written in C# and invokes a JDK method. However, when I run the app, it throws a System.TypeInitializationException.
Unhandled Exception: System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
at System.ArgumentNullException.Throw(String) + 0x2b
at System.IO.Path.Combine(String, String) + 0x23
at IKVM.Runtime.JVM.Properties.GetHomePath() + 0x157
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode) + 0xb0
at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean) + 0x77
at System.Lazy`1.CreateValue() + 0xc8
at IKVM.Runtime.JVM.Internal..cctor() + 0xc9
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb9
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14a
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
at IKVM.Runtime.JVM.Init() + 0x1b4
at IKVM.Runtime.RuntimeInit.Init() + 0x66
at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunInitializers(TypeManagerHandle, ReadyToRunSectionType) + 0x39
at Internal.Runtime.CompilerHelpers.StartupCodeHelpers.RunModuleInitializers() + 0x36
at IKVMDemoAppNativeAotLibrary!<BaseAddress>+0x575887
Warnings during AOT compilation:
warning IL3000: IKVMDemoAppNativeAotLibrary.Program
.GetHomePath(): 'System.Reflection.Assembly.Location.get' always returns an empty string for assemblies embedded in a s
ingle-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. [C:\Pro
jects\IKVMDemoApp\IKVMDemoAppNativeAotLibrary\IKVMDemoAppNativeAotLibrary.csproj]
ILC : warning IL3000: IKVM.Runtime.JVM.Properties.GetIkvmProperties(): 'System.Reflection.Assembly.Location.get' always
returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, cons
ider calling 'System.AppContext.BaseDirectory'. [C:\Projects\IKVMDemoApp\IKVMDemoAppNativeAotLibrary\IKVMDemoAppNativeA
otLibrary.csproj]
ILC : warning IL3000: IKVM.Runtime.JVM.Properties.GetHomePath(): 'System.Reflection.Assembly.Location.get' always retur
ns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider c
alling 'System.AppContext.BaseDirectory'. [C:\Projects\IKVMDemoApp\IKVMDemoAppNativeAotLibrary\IKVMDemoAppNativeAotLibr
ary.csproj]
Dynamic linking has the same outcome as static linking.
Steps to reproduce
- Create a C# console app that uses IKVM to call a JDK method. For example:
namespace IKVMDemoApp;
using System = java.lang.System;
public class ConsoleApp {
public static void main(String[] args) {
[email protected]("Hello, World!");
}
}
- Create a C# class library that exports the main function. For example:
namespace IKVMDemoAppNativeAotLibrary {
public class Exports {
[UnmanagedCallersOnly(EntryPoint = nameof(main0))]
public static void main0() {
var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
ConsoleApp.main(args);
}
}
}
- AOT compile the class library to IKVMDemoAppNativeAotLibrary.lib using the dotnet publish command with the nativeaot option.
dotnet publish -c Release -p:NativeLib=Static --use-current-runtime
- Create a C/C++ program that consumes the IKVMDemoAppNativeAotLibrary.lib and calls the main0 function.
extern void main0(void);
int main(void) {
main0();
}
- Compile and link the C/C++ program with the IKVMDemoAppNativeAotLibrary.lib and other necessary libraries.
bootstrapperdll.obj
Runtime.WorkstationGC.lib
eventpipe-disabled.lib
Runtime.VxsortEnabled.lib
System.Globalization.Native.Aot.lib
System.IO.Compression.Native.Aot.lib
bcrypt.lib
- Run the executable and observe the error message.
As neat as it would be for NAOT to work on IKVM, it is likely darn near impossible as it stands, and very low priority. I'll leave this open, since it's something to tackle some day..... But that's probably way down the road.
the same as https://github.com/ikvmnet/ikvm/issues/498