从Java调用.NET程序集:JVM崩溃 [英] Calling .NET assembly from Java: JVM crashes

查看:225
本文介绍了从Java调用.NET程序集:JVM崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方的.NET程序集和一个大型的Java应用程序。我需要调用由.NET类库从Java应用程序提供mothods。未启用COM的装配。
我已经搜查了网,到目前为止,我有以下几点:

I have a third party .NET Assembly and a large Java application. I need to call mothods provided by the .NET class library from the Java application. The assembly is not COM-enabled. I have searched the net and so far i have the following:

C#代码(cslib.cs):

C# code (cslib.cs):

using System;

namespace CSLib
{
    public class CSClass
    {
        public static void SayHi()
        {
            System.Console.WriteLine("Hi");
        }
    }
}



与(使用.NET编译3.5,但同样的情况发生在使用2.0):

compiled with (using .net 3.5, but the same happens when 2.0 is used):

csc /target:library cslib.cs

C ++代码(clib.cpp):

C++ code (clib.cpp):

#include <jni.h>
#using <CSLib.dll>

using namespace CSLib;

extern "C" _declspec(dllexport) void Java_CallCS_callCS(JNIEnv* env, jclass cls) {
    CSLib::CSClass::SayHi();
}



与(用VC 2008工具进行编译,但同样的情况发生时,2003的工具是使用):

compiled with (using VC 2008 tools, but the same happens when 2003 tools are used):

cl /clr /LD clib.cpp
mt -manifest clib.dll.manifest -outputresource:clib.dll;2

Java代码(CallCS.java):

Java code (CallCS.java):

class CallCS {
    static {
       System.loadLibrary("clib");
    }
    private static native void callCS();
    public static void main(String[] args) {
        callCS();
    }
}

当我尝试运行Java类中,Java VM崩溃而调用方法(它是能够加载库):

When I try to run the java class, the Java VM crashes while invoking the method (it is able to load the library):


#
# An unexpected error has been detected by Java Runtime Environment:
#
#  Internal Error (0xe0434f4d), pid=3144, tid=3484
#
# Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode, sharing windows-x86)
# Problematic frame:
# C  [kernel32.dll+0x22366]
#
...
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  CallCS.callCS()V+0
j  CallCS.main([Ljava/lang/String;)V+0
v  ~StubRoutines::call_stub

不过,如果我创建加载clib.dll并调用导出函数Java_CallCS_callCS一个普通CPP的应用程序,一切为确定。
我已经在x86和x64环境中尝试这样做的结果是一样的。我还没有尝试过的Java的其他版本,但我需要的代码在1.5.0运行。

However, if I create a plain cpp application that loads clib.dll and calls the exported function Java_CallCS_callCS, everything is OK. I have tried this on both x86 and x64 environments and the result is the same. I have not tried other versions of Java, but I need the code to run on 1.5.0.

此外,如果我修改clib.cpp调用的系统方法的一切工作正常甚至从Java:

Moreover, if I modify clib.cpp to call only System methods everything works fine even from Java:

#include <jni.h>
#using <mscorlib.dll>

using namespace System;

extern "C" _declspec(dllexport) void Java_CallCS_callCS(JNIEnv* env, jclass cls) {
    System::Console::WriteLine("It works");
}

要包:


  1. 我能够从Java调用系统的方法 - > clib.dll - > mscorlib.dll中

  2. 我能够调用从CPPApp任何方法 - > clib.dll - > cslib.dll

  3. 我无法从Java调用任何方法 - > clib.dll - > cslib.dll

据我所知,上述用途1.一个解决办法 - 我可以使用反射只使用系统调用来加载assmebly并调用所需的方法,但是代码变得混乱,我希望为更好的解决方案。

I am aware of a workaround that uses 1. above - I can use reflection to load the assmebly and invoke desired methods using only System calls, but the code gets messy and I am hoping for a better solution.

我知道dotnetfromjava项目,该项目采用反射法,但不希望增加更多的比需要的复杂性。如果没有别的办法,但我会用这样的事情。

I know about dotnetfromjava project, which uses the reflection method, but prefer not to add more complexity than needed. I'll use something like this if there is no other way, however.

我在ikvm.net看着还可以,但我的理解是,它使用自己的JVM (C#编写的)做的魔力。但是,运行在其虚拟机的整个Java应用程序是我没有选择。

I have looked at ikvm.net also, but my understanding is that it uses its own JVM (written in C#) to do the magic. However, running the entire Java application under its VM is no option for me.

感谢。

推荐答案

OK,奥秘就解决了。

OK, the mystery is solved.

JVM的崩溃是由未处理System.IO.FileNotFoundException引起的。因为.NET程序集搜索,其中调用exe文件所在的文件夹中的异常。

The JVM crash is caused by unhandled System.IO.FileNotFoundException. The exception is thrown because the .NET assembly is searched in the folder where the calling exe file resides.


  1. mscorlib.dll程序是在全局程序集缓存,所以它的工作原理。

  2. 的CPP应用程序exe文件是在同一文件夹中装配的,所以它的工作原理也。

  3. 的cslib.dll装配既不的java.exe的文件夹中,也不在GAC,所以这是行不通的。

看来我唯一的选择就是安装.NET程序集在GAC(第三方DLL确实有很强的名称)。

It seems my only option is to install the .NET assembly in GAC (the third-party dll does have a strong name).

这篇关于从Java调用.NET程序集:JVM崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆