csharp 到 java JNI 移植调用以在 ubuntu 上运行 [英] csharp to java JNI porting call to run on ubuntu

查看:62
本文介绍了csharp 到 java JNI 移植调用以在 ubuntu 上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误:崩溃发生在本地代码中的 Java 虚拟机之外.每当我使用带有本机库和 .net 模块文件的类文件运行时,它都可以正常工作.但是当我尝试单独运行类文件和本机库时,它会崩溃.请澄清我所做的错误,请查看我的代码.供您参考参数

============================================

 公共类 Sum{public int add(int a, int b){返回 a + b;}}

==============================================

另存为Sum.cs并编译成模块使用命令:

<块引用>

csc/t:module sum.cs

创建 Java 文件进行测试

==============================================

公开课测试{public native int add(int a,int b);静止的 {System.loadLibrary("JSample");}公共静态无效主(字符串 [] args){System.out.println(new test().add(10,15));}}

============================================

另存为 test.java 编译为

<块引用>

javac test.java

创建原生头文件

<块引用>

javah -jni 测试

它将创建 test.h

使用visual studio创建win32项目(我用的是VS2010)选择项目名称为JSample

包含头文件和 C#.net 模块为托管 C++ 转换编写标头

================================================/p>

#using #使用Sum.netmodule"使用命名空间系统;公共 __gc 类 SumC{民众:总和 __gc *t;SumC(){t = 新总和();}int callCSharpSum(int a,int b){返回 t->add(a,b);}};

==============================================

另存为 sum.h

创建 sum.cpp 文件

============================================

#include #include "test.h"#include "sum.h"JNIEXPORT jint JNICALL Java_test_add(JNIEnv *, jobject, jint a, jint b){SumC* t = new SumC();返回 t->callCSharpSum(a,b);}

==============================================

优化编译器构建/clr:oldSyntax包含 Jdk/包含目录路径构建项目.我们将获得 JSample DLL

运行项目与C#.net 模块,Native DLL 文件和类文件在同一文件夹中.

<块引用>

java 测试

25

但是每当我使用单独的dll文件和类文件运行代码时.它显示错误报告崩溃发生在本机代码中的 Java 虚拟机之外.请阐明如何将托管代码 C# 移植到 C++(Win32 非托管代码)中.库文件.

解决方案

要使用 Java 中的 .NET 程序集,我强烈建议您查看 IKVM,这是一个桥接到 .NET 运行时的 Java VM.

我在(我认为)2004 年将其用于生产软件,并且运行良好.该项目目前得到了积极维护,并获得了对 .NET 4 和 Java 7 的支持.

您可以选择

  • 在 IKVM 中运行 Java 代码,以便您可以使用 .NET 库
  • 运行加载 Java 库(例如 jar)的 .NET 程序

无论哪种方式,都有一个预处理步骤将 jar 转换为 DLLS,反之亦然.

参见 http://ikvm.net

I getting bug: crash happened outside the Java Virtual Machine in native code. whenever i run with class file with native library and .net module file it works fine. but when i try to run alone class file and native library it gets crash .please clarify my mistake i have done, please review my code. for your reference with parameter

==========================================

 public class Sum
    {
        public int add(int a, int b)
        {
            return a + b;
        }
    }

===========================================

save as Sum.cs and compile it to module using cmd:

csc /t:module sum.cs

Create Java File to test

===========================================

public class test{
public native int add(int a,int b);
 static {
        System.loadLibrary("JSample");
    }

    public static void main (String[] args) {
       System.out.println(new test().add(10,15));
    }
}

==========================================

save it as test.java compile as

javac test.java

create native header file

javah -jni test

it will create test.h

create win32 project using visual studio (I used VS2010) Choose project name as JSample

include header and C#.net module write header for manged C++ conversion

==============================================

#using <mscorlib.dll>
#using "Sum.netmodule"
using namespace System;
public __gc class SumC
{
public:
    Sum __gc *t;
    SumC()
    {
        t = new Sum();          
    }
    int callCSharpSum(int a,int b)
    {
        return  t->add(a,b);
    }
};

===========================================

save it as sum.h

create sum.cpp file

============================================

#include <jni.h>
#include "test.h"
#include "sum.h"

JNIEXPORT jint JNICALL Java_test_add
    (JNIEnv *, jobject, jint a, jint b)
{
    SumC* t = new SumC();  
    return t->callCSharpSum(a ,b );
}

=============================================

optimize compiler to build /clr:oldSyntax Include Jdk/Include directory path build the project. we will Get JSample DLL

run the project with C#.net module,Native DLL file and class file at the same folder.

java test

25

but whenever i run the code with dll file and class file alone. it shows bug report The crash happened outside the Java Virtual Machine in native code. please clarify how to port managed code C# into C++ (Win32 Un-Managed code). library file.

解决方案

To use .NET assemblies from Java, I strongly suggest you look at IKVM, which is a Java VM that bridges to .NET Runtime.

I've used this back in (I think) 2004 for production software and it worked nicely. The project is actively maintained and recieves support for .NET 4 and Java 7 these days.

You have a choice of

  • running the Java code in IKVM so you can use .NET libraries
  • run a .NET program that loads java libraries (e.g. jars)

Both ways, there is a preprocessing step to translate the jars to DLLS or viceversa.

See http://ikvm.net

这篇关于csharp 到 java JNI 移植调用以在 ubuntu 上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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