C# csharp to java JNI移植调用在ubuntu上运行
我发现了一个bug:崩溃发生在Java虚拟机之外的本机代码中。每当我运行带有本机库和.net模块文件的类文件时,它都可以正常工作。 但当我试图单独运行类文件和本机库时,它会崩溃。请澄清我犯的错误,请检查我的代码。 供您使用参数参考 ==========================================C# csharp to java JNI移植调用在ubuntu上运行,c#,java,ubuntu,managed-c++,C#,Java,Ubuntu,Managed C++,我发现了一个bug:崩溃发生在Java虚拟机之外的本机代码中。每当我运行带有本机库和.net模块文件的类文件时,它都可以正常工作。 但当我试图单独运行类文件和本机库时,它会崩溃。请澄清我犯的错误,请检查我的代码。 供您使用参数参考 ========================================== public class Sum { public int add(int a, int b) { return a
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
===========================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
另存为Sum.cs并将其编译为模块
使用cmd:
csc/t:模块sum.cs
创建要测试的Java文件
===========================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
==========================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
另存为test.java编译为
javac test.java
创建本机头文件
javah-jni测试
它将创建test.h
使用visual studio创建win32项目(我使用的是VS2010)
选择项目名称作为JSample
包括标头和C#.net模块
写C++标题转换C++ < /p>
==============================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
#使用
#使用“Sum.netmodule”
使用名称空间系统;
公共uu gc类SumC
{
公众:
总和u gc*t;
SumC()
{
t=新和();
}
int callCSharpSum(int a,int b)
{
返回t->add(a,b);
}
};
===========================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
另存为sum.h
创建sum.cpp文件
============================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
#包括
#包括“test.h”
#包括“sum.h”
JNIEXPORT jint JNICALL Java\u测试\u添加
(JNIEnv*,jobject,jint a,jint b)
{
SumC*t=新的SumC();
返回t->callCSharpSum(a,b);
}
=============================================
public class Sum
{
public int add(int a, int b)
{
return a + b;
}
}
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));
}
}
#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);
}
};
#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 );
}
优化编译器以生成/clr:oldSyntax
Include Jdk/Include目录路径
构建项目。
我们将获得JSample DLL
运行项目
使用C#.net模块时,本机DLL文件和类文件位于同一文件夹中
java测试
二十五
但是每当我单独使用dll文件和类文件运行代码时。
它以本机代码显示崩溃发生在Java虚拟机外部的错误报告。
请说明如何将托管代码C++(C++)导入C++(Win32 Un托管代码)。库文件。要使用来自Java的.NET程序集,我强烈建议您看看IKVM,它是一种连接到.NET运行时的Java虚拟机 我在(我想)2004年的生产软件中使用过这个,它工作得很好。该项目目前正在积极维护,并获得了对.NET4和Java7的支持 你可以选择
- 在IKVM中运行Java代码以便可以使用.NET库
- 运行加载java库(例如JAR)的.NET程序
请参见要简化示例,您不需要在任何地方创建(或释放)对象。我只有一个可以工作的Linux,所以我不知道你的错误是什么,也许你可以发布一个简短的摘要,让每个人都知道你在说什么。感谢回复sehe,但我的目标是让.net管理代码完成非托管代码(纯win32库),能够在win32系统上运行,而无需.net运行时环境。到目前为止,ubuntu是我的第二个。所以文件通常只能在基于linux的系统上运行。@arunkumarnonascii:我不知道你想说什么,但IKVM和Mono(.NET)在linux上运行得很好。我已经在linux上的IKVM上运行了整个eclipse IDE,以使用eclipse插件中的托管代码。。。我几乎只使用Ubuntu,我自己