原帖地址:https://bbs.csdn.net/topics/390624108
总体分三步走:
- 准备一个 C# 类库 (dll)
- 编写一个java 类
- 编译 java 类并打包成jar,即可执行
第一步,我的 C#类库代码
Calc.cs 代码如下:
using System;
namespace MyCSharpDemoCalc
{
public interface ICalc
{
int MySuperSmartFunctionIDontHaveInJava(int mi);
string PrintPatientInfo(patient aPatient);
}
public class DemoCalc : ICalc
{
private readonly Random r = new Random();
private patient Patient;
public DemoCalc(patient aPatient)
{
Patient = new patient();
Patient.ID = aPatient.ID;
Patient.FirstName = aPatient.FirstName;
Patient.Sex = aPatient.Sex;
}
public int MySuperSmartFunctionIDontHaveInJava(int mi)
{
return mi + r.Next();
}
public string PrintPatientInfo(patient aPatient)
{
return aPatient.ID.ToString() + " - " + aPatient.FirstName + " - " + aPatient.Sex.ToString();
}
}
}
patient.cs 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyCSharpDemoCalc
{
public class patient
{
private int id;
private string firstname;
private bool sex;
public int ID
{
get { return id; }
set { id = value; }
}
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public bool Sex
{
get { return sex; }
set { sex = value; }
}
}
}
编译成一个 C# 的 dll比如 MyCSharpDemoCalc.dll 即可,FrameWork用什么版本都可以
第二步、编写一个java 类,代码如下:
MyCalcUsageInJava.java
import net.sf.jni4net.Bridge;
import java.io.IOException;
import mycsharpdemocalc.DemoCalc;
import mycsharpdemocalc.ICalc;
import mycsharpdemocalc.patient;
public class MyCalcUsageInJava {
public static void main(String arsg[]) throws IOException {
Bridge.init();
//加载 C# 类库
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("MyCSharpDemoCalc.j4n.dll"));
//实例化 C# 类库中的类 patient
patient Patient = new patient();
Patient.setID(1);
Patient.setFirstName("张三");
Patient.setSex(true);
//实例化 C# 类库中的类 DemoCalc
ICalc calc = new DemoCalc(Patient);
//调用 C# 类库中 DemoCalc 类的方法MySuperSmartFunctionIDontHaveInJava
final int result = calc.MySuperSmartFunctionIDontHaveInJava(123);
System.out.printf("执行结果 : " + result + "\r\n");
//调用 C# 类库中 DemoCalc 类的方法PrintPatientInfo
final String result1 = calc.PrintPatientInfo(Patient);
System.out.printf("执行结果 : " + result1);
}
}
第三步、编译我的java文件 MyCalcUsageInJava.java
分几小步
A)下载 jni4net http://sourceforge.net/projects/jni4net/?source=dlp 解压
B)在解压目录下建立sample目录
C)在 sample目录 中 建立 mycalc目录
D)在 sample目录 中 建立 work 子目录 将 jni4net lib目录中的 jni4net.n.w32.v20-0.8.6.0.dll、jni4net.j-0.8.6.0.jar、jni4net.n.w32.v40-0.8.6.0.dll、jni4net.n.w64.v20-0.8.6.0.dll、jni4net.n.w64.v40-0.8.6.0.dll、jni4net.n-0.8.6.0.dll 拷贝进来
E) 将编译好的 C# 类库 MyCSharpDemoCalc.dll 也拷贝到上面的 work子目录中
F)在 work目录中建立 build.cmd
@echo off
if not exist target mkdir target
if not exist target\classes mkdir target\classes
echo compile classes
javac -nowarn -d target\classes -sourcepath jvm -cp "e:\javacallc#\jni4net-0.8.6.0-bin\lib\jni4net.j-0.8.6.0.jar"; "jvm\mycsharpdemocalc\ICalc.java" "jvm\mycsharpdemocalc\ICalc_.java" "jvm\mycsharpdemocalc\DemoCalc.java" "jvm\mycsharpdemocalc\patient.java"
IF %ERRORLEVEL% NEQ 0 goto end
echo MyCSharpDemoCalc.j4n.jar
jar cvf MyCSharpDemoCalc.j4n.jar -C target\classes "mycsharpdemocalc\ICalc.class" -C target\classes "mycsharpdemocalc\ICalc_.class" -C target\classes "mycsharpdemocalc\__ICalc.class" -C target\classes "mycsharpdemocalc\DemoCalc.class" -C target\classes "mycsharpdemocalc\patient.class" > nul
IF %ERRORLEVEL% NEQ 0 goto end
echo MyCSharpDemoCalc.j4n.dll
csc /nologo /warn:0 /t:library /out:MyCSharpDemoCalc.j4n.dll /recurse:clr\*.cs /reference:"E:\javaCallC#\jni4net-0.8.6.0-bin\samples\myCSharpDemoCalc\work2\MyCSharpDemoCalc.dll" /reference:"E:\javaCallC#\jni4net-0.8.6.0-bin\lib\jni4net.n-0.8.6.0.dll"
IF %ERRORLEVEL% NEQ 0 goto end
:end
G)在mycalc目录中建一个 java ant 编译脚本 build.bat
@echo off
copy ..\..\lib\*.* work
..\..\bin\proxygen.exe work\MyCSharpDemoCalc.dll -wd work2
cd work
call build.cmd
cd ..
echo compiling usage
javac -d work\ -cp work\jni4net.j-0.8.6.0.jar;work\MyCSharpDemoCalc.j4n.jar MyCalcUsageInJava.java
G)在cmd dos窗口中运行 mycalc目录 中的 build.bat即可进行编译l了,编译效果:
H)运行编译好的 jar包看看效果:
cd work
java -cp jni4net.j-0.8.6.0.jar;MyCSharpDemoCalc.j4n.jar;. MyCalcUsageInJava
今后只需要更新 C# dll即可,java代码无需重新编译,简单吧,很爽吧!
更多详情:http://kan.weibo.com/con/3636924272047381
有任何疑问欢迎随时找我交流。