java怎么调用C的dll
我做了1个java调用c++的dll
想做一个java调用C的dll就不知道怎么做了
在xx.c里 写
JNIEXPORT jint JNICALL Java_testdll_getValue
(JNIEnv *, jobject)
这些报错 之前在XX.CPP里写可以 c语言不了解 谁有
java调C的dll的例子呀,或者给我讲讲也行 谢谢
[解决办法]
好像是 System.loadLibrary这个吧。
我们的项目里有一段,贴出来参考一下吧
package org.scavino.twain;
import ins.filemanager.fileuploader.FileUploaderConst;
import ins.filemanager.fileuploader.FileUploaderFrame;
import ins.filemanager.fileuploader.FileUploaderParams;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JTwain {
private static final Log logger = LogFactory
.getLog(JTwain.class);
private static final JTwain mInstance = new JTwain();
protected final String DLL_NAME = "jtwain";
private JTwain() {
initLib();
}
public static JTwain getInstance(){
return mInstance;
}
public native boolean isTwainAvailble();
public native String[] getAvailableSources();
public native String[] acquire();
public native String[] acquire(String sourceName);
/*
* Loads the C/JNI Libray
*/
private void initLib(){
try {
String libraryPath = System.getProperty("java.library.path");
if ((libraryPath == null)
[解决办法]
(libraryPath.trim().length() == 0)) {
logger.info("");
return;
}
String systemDir = null;
String firstEntry = null;
String original;
String entry;
StringTokenizer st = new StringTokenizer(libraryPath, ";");
int i = 0;
while (i < st.countTokens()) {
original = st.nextToken();
entry = original;
if (i == 0) {
firstEntry = entry;
}
if (entry.endsWith("\")) {
entry = entry.substring(0, entry.length() - 1);
}
entry = entry.toLowerCase();
if (entry.endsWith("windows\\system")) {
systemDir = original;
break;
}
if (entry.endsWith("winnt\\system32")) {
systemDir = original;
break;
}
if (entry.endsWith("windows\\system32")) {
systemDir = original;
File file = new File(original.replace("System32", "SysWow64"));
if(file.exists()){
systemDir = file.getPath();
}
break;
}
i++;
}
if (systemDir == null) {
systemDir = firstEntry;
}
installFile(new URL(FileUploaderConst.FileUploadURL), systemDir
+ "/jtwain.dll");
System.loadLibrary(DLL_NAME);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e);
}
finally {
// Send to your logging subsystem
System.out.println("Loading : " + DLL_NAME + ".dll");
}
}
/**
* dll
*/
protected static void installFile(URL sourceUrl, String destFileName) {
File destFile = new File(destFileName);
if (!destFile.exists()) {
try {
System.err.println("installing file " + destFileName);
destFile.getParentFile().mkdirs();
URLConnection connection = sourceUrl.openConnection();
InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(destFile);
byte[] buff = new byte[8192];
BufferedInputStream in = new BufferedInputStream(is,
buff.length);
BufferedOutputStream out = new BufferedOutputStream(fos,
buff.length);
int i;
int count = 0;
while ((i = in.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, i);
count += i;
}
in.close();
out.close();
} catch (Exception exception) {
exception.printStackTrace();
logger.error(exception);
}
}
}
}