1、先生成目标dll库(例如MesCommonLib.dll) 2、下载jni4net-0.8.8.0-bin, 在其bin目录下,控制台执行:
- ./proxygen.exe .\MesCommonLib.dll -wd .\folder -cp .\dependence1.dll .\dependence2.dll
- -wd 是指导输出目录
- -cp 指定目标jar依赖的jar包,多个依赖用“;”号隔开。目标jar依赖的dll库用 空格隔开
3、在.\folder路径下双击build.exe,正常情况下会产生一个MesCommonLib.j4n.dll和一个MesCommonLib.j4n.jar 4、将jni4net.n-0.8.8.0.dll,Jni4NetDll.dll,jni4net.n.w64.v40-0.8.8.0.dll(四个全部),MesCommonLib.dll,MesCommonLib.j4n.dll等库及其依赖库复制到java项目根目录下 5、java项目添加jar包jni4net.j-0.8.8.0.jar,MesCommonLib.j4n.jar 6 java代码 Bridge.setVerbose(true); Bridge.init(); File loadDll = new File("MesCommonLib.j4n.dll"); Bridge.LoadAndRegisterAssemblyFrom(loadDll); CBarcodePrinter test = new CBarcodePrinter(null); 7、测试
---------------------------------------------------------------------------------
jni4net的帮助输入: .\proxygen.exe -h
jni4net.proxygen - Copyright (C) 2009 Pavel Savara - licensed under GPLv3
usage: proxygen.exe path\to\.xml usage: proxygen.exe path\to\.jar -wd work\directory -cp java\dependencies;comma\separated;classpath -dp .NET\dependencies;comma\separated;assemblies usage: proxygen.exe path\to\.dll -wd work\directory -cp java\dependencies;comma\separated;classpath -dp .NET\dependencies;comma\separated;assemblies
参考- 如何使用jni4net实现java调用c#的dll
- jni4net 实现 c# 调用 java jar包
- jni4net调用net库
--------------------------------------------------------------------------------
使用命令备份:将要转换的目标的dll,以及依赖的dll,赋值到目录 jni4net-0.8.8.0-bin\bin\ 下,然后在此目录下cmd窗口执行如下命令,生成 folder_SI_1.3文件夹
.\proxygen.exe .\SusceptibilityIndexCalculation1.3.dll -wd .\folder_SI_1.3 -cp .\cairo.dll .\cfitsio.dll .\expat.dll .\freexl.dll .\fribidi.dll .\gdal202.dll .\gdalconst_csharp.dll .\gdalconst_wrap.dll .\gdal_csharp.dll .\gdal_wrap.dll .\geos.dll .\geos_c.dll .\hdf5.dll .\hdf5_cpp.dll .\hdf5_hl.dll .\hdf5_hl_cpp.dll .\hdfdll.dll .\iconv.dll .\libcurl.dll .\libeay32.dll .\libecwj2.dll .\libfcgi.dll .\libkea.dll .\libmysql.dll .\libpq.dll .\libtiff.dll .\libxml2.dll .\lti_dsdk_9.5.dll .\lti_lidar_dsdk_1.1.dll .\mapserver.dll .\mfhdfdll.dll .\msplugin_mssql2008.dll .\msplugin_oracle.dll .\msvcp140.dll .\NCSEcw.dll .\netcdf.dll .\ogr_csharp.dll .\ogr_wrap.dll .\openjp2.dll .\osr_csharp.dll .\osr_wrap.dll .\proj.dll .\spatialite.dll .\sqlite3.dll .\ssleay32.dll .\szip.dll .\tbb.dll .\vcruntime140.dll .\xdrdll.dll .\xerces-c_3_1.dll .\zlib1.dll .\Meta.Numerics.dll
生成的文件夹 folder_SI_1.3下,生成 两个文件:SusceptibilityIndexCalculation1.3.j4n.dll 和 SusceptibilityIndexCalculation1.3.j4n.jar
.\build.cmd
将 要转换的目标的dll、以及依赖的dll、SusceptibilityIndexCalculation1.3.j4n.dll、SusceptibilityIndexCalculation1.3.j4n.jar,复制到java项目的lib文件夹下,
在Java Build Path -> Libraries 中,点击 Add External JARS, 添加jar下面两个包:a. FrequencyRatiosCaculation.j4n.jar b. jni4net.j-0.8.8.0.jar。使用如下代码即可调用c# dll中 public 的接口了
import net.sf.jni4net.Bridge;
import alsa.ALSACalSIConverter; # c# dll中 public的类
public class InvokeTest {
public static void main(String ards[]) throws IOException
{
//invoke c# dll
Bridge.setVerbose(true);
Bridge.init();
File loadDll = new File("lib/SusceptibilityIndexCalculation1.3.j4n.dll");
Bridge.LoadAndRegisterAssemblyFrom(loadDll);
//TODO something
}
}