java中JNative调用dll绝对路径为啥找不到dll
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中JNative调用dll绝对路径为啥找不到dll相关的知识,希望对你有一定的参考价值。
有以下几种方法:1、把.dll文件放在java jdk的bin目录下面就行了
2、配置运行文件的-path路径,在里面加入.dll的绝对路径
3、将.dll文件复制到你编译好的.class目录下追问
第一种方法我也尝试过,但是一样的调用不到,后来不知道怎么回事又可以调用的到了,不知道是不是环境的问题呢?
追答我也遇到过这样的情况,不过当时重启了下电脑就好了,具体还不知道什么原因,现在能用就行了
参考技术A 你别用绝对路径~直接用System.loadLibrary("dll名字");就行了。XP的话~把dll放到system32下面哦~追问嗯,你的这个方法我也试了,但是我的dll依赖另一个dll,并且必须要用绝对路径,我试了几次也不知道怎么回事,突然就好了,可以调用的到了。。。
参考技术B 可以的啊使用JNative对条码打印机进行打印
因项目需要,对Godex EZ2300条码机进行调用打印, 驱动为Ez2000.dll, 用JNI可以实现调用,但比较麻烦, 需
要用到VC6进行DLL封装,改用JNative组件进行调用, 步骤如下:
1. 下载JNative-1.3.2.zip组件库
2. 解压出来后有JNative.jar / JNativeCpp.dll / libJNativeCpp.so三个文件,JNative.jar置于%JAVA_HOME%/jre/lib/ext目录下, JNativeCpp.dll置于%SYSTEM%/WINDOWS/SYSTEM32目录下
3.将Ez2000.dll置于%SYSTEM%/WINDOWS/SYSTEM32目录以及%JAVA_HOME%/jdk/bin目录下
4.
具体Java代码如下:
package
com.athens.test;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class Demo {
private static final String PRINTER_DLL = " Ez2000.dll " ;
public static void main(String [] args) {
Demo demo = new Demo();
System.loadLibrary( " Ez2000 " );
JNative n = null ;
try {
n = demo.getJNativeByFunction( " isready " );
n.setRetVal(Type.INT);
// n.setParameter(1, Type.VOID, "");
n.invoke();
Integer isready = Integer.parseInt(n.getRetVal());
if (isready == 1 ){
System.out.println( " 条码机可以开始接收资料!Starting... " );
System.out.println( " Starting... " );
// 打开端口
n = demo.getJNativeByFunction( " openport " );
n.setParameter( 0 , Type.STRING, " 0 " );
n.invoke();
// 设定参数
n = demo.getJNativeByFunction( " setup " );
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.INT, " 30 " );
n.setParameter( 0 , Type.INT, " 7 " );
n.setParameter( 0 , Type.INT, " 2 " );
n.setParameter( 0 , Type.INT, " 1 " );
n.setParameter( 0 , Type.INT, " 0 " );
n.setParameter( 0 , Type.INT, " 0 " );
n.invoke();
// 发出指令
n = demo.getJNativeByFunction( " sendcommand " );
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " W70 " );
n.invoke();
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " ^P1 " );
n.invoke();
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " ^L " );
n.invoke();
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " AC,20,60,1,1,1,0,TEST " );
n.invoke();
// 发送条形码
n = demo.getJNativeByFunction( " ecTextOut " );
n.setParameter( 0 , Type.INT, " 20 " );
n.setParameter( 1 , Type.INT, " 10 " );
n.setParameter( 2 , Type.INT, " 34 " );
n.setParameter( 3 , Type.STRING, " 标楷体 " );
n.setParameter( 4 , Type.STRING, " Athens Testing Successlly!!! " );
n.invoke();
// 发出指令
n = demo.getJNativeByFunction( " sendcommand " );
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " E " );
n.invoke();
// 关闭端口
n = demo.getJNativeByFunction( " closeport " );
n.invoke();
System.out.println( " Ending... " );
} else {
System.out.println( " 条码机无法接收资料! " );
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (n != null ){
try {
n.dispose();
} catch (NativeException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
// 通过方法名取得JNative对象
public JNative getJNativeByFunction(String functionName) throws NativeException{
return new JNative(PRINTER_DLL, functionName);
}
}
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class Demo {
private static final String PRINTER_DLL = " Ez2000.dll " ;
public static void main(String [] args) {
Demo demo = new Demo();
System.loadLibrary( " Ez2000 " );
JNative n = null ;
try {
n = demo.getJNativeByFunction( " isready " );
n.setRetVal(Type.INT);
// n.setParameter(1, Type.VOID, "");
n.invoke();
Integer isready = Integer.parseInt(n.getRetVal());
if (isready == 1 ){
System.out.println( " 条码机可以开始接收资料!Starting... " );
System.out.println( " Starting... " );
// 打开端口
n = demo.getJNativeByFunction( " openport " );
n.setParameter( 0 , Type.STRING, " 0 " );
n.invoke();
// 设定参数
n = demo.getJNativeByFunction( " setup " );
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.INT, " 30 " );
n.setParameter( 0 , Type.INT, " 7 " );
n.setParameter( 0 , Type.INT, " 2 " );
n.setParameter( 0 , Type.INT, " 1 " );
n.setParameter( 0 , Type.INT, " 0 " );
n.setParameter( 0 , Type.INT, " 0 " );
n.invoke();
// 发出指令
n = demo.getJNativeByFunction( " sendcommand " );
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " W70 " );
n.invoke();
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " ^P1 " );
n.invoke();
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " ^L " );
n.invoke();
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " AC,20,60,1,1,1,0,TEST " );
n.invoke();
// 发送条形码
n = demo.getJNativeByFunction( " ecTextOut " );
n.setParameter( 0 , Type.INT, " 20 " );
n.setParameter( 1 , Type.INT, " 10 " );
n.setParameter( 2 , Type.INT, " 34 " );
n.setParameter( 3 , Type.STRING, " 标楷体 " );
n.setParameter( 4 , Type.STRING, " Athens Testing Successlly!!! " );
n.invoke();
// 发出指令
n = demo.getJNativeByFunction( " sendcommand " );
n.setRetVal(Type.INT);
n.setParameter( 0 , Type.STRING, " E " );
n.invoke();
// 关闭端口
n = demo.getJNativeByFunction( " closeport " );
n.invoke();
System.out.println( " Ending... " );
} else {
System.out.println( " 条码机无法接收资料! " );
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (n != null ){
try {
n.dispose();
} catch (NativeException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
// 通过方法名取得JNative对象
public JNative getJNativeByFunction(String functionName) throws NativeException{
return new JNative(PRINTER_DLL, functionName);
}
}
以上是关于java中JNative调用dll绝对路径为啥找不到dll的主要内容,如果未能解决你的问题,请参考以下文章