smh_5800
چهارشنبه 03 آذر 1389, 10:36 صبح
سلام :
من از یک نمونه برنامه در مورد نحوه کار JNI و استفاده از DLL در جاوا استفاده می کنم که به درستی اجرا می شود :
/* Test.java */
public class Test {
/*Load the dll that exports functions callable from java*/
static {System.loadLibrary("TestImp");} /* Name Of DLL : TestImp.dll */
/*Imported function declarations*/
public native void print(String msg);
public native byte[] readFile(String path);
public native int searchFile(String path, byte[] b, int bLength);
public native String[] findFiles(String path, String mask);
public native String[] checkProcess(String processName, boolean killIt, boolean printOutput);
public native int startProcess(String commandLine, String workingDir);
public native boolean waitForFileMask(String directrory, String fileMask);
public native boolean waitForAnyFile(String directrory);
public void Test() {
}
public static void main(String [] args) {
Test t = new Test();
/*Printf example*/
t.print("->Testing JNI - Hello from \n");
/*Loading a file as a byte array example*/
System.out.println("->Start Open file Text.txt\n" + new String(t.readFile("Text.txt")));
/*Printf example*/
t.print("->Finish Sample \n");
}
}
همین طور که ملاحظه می کنید Test.java فاقد package بوده ، برنامه فوق را به صورت زیر تغییر دادم :
( مسیر فایل : C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes\ver )
/* Test .java*/
package ver;
public class Test {
/*Load the dll that exports functions callable from java*/
static {System.loadLibrary("TestImp");} /* Name Of DLL : TestImp.dll */
/*Imported function declarations*/
public native void print(String msg);
public native byte[] readFile(String path);
public native int searchFile(String path, byte[] b, int bLength);
public native String[] findFiles(String path, String mask);
public native String[] checkProcess(String processName, boolean killIt, boolean printOutput);
public native int startProcess(String commandLine, String workingDir);
public native boolean waitForFileMask(String directrory, String fileMask);
public native boolean waitForAnyFile(String directrory);
public void Test() {
}
}
و در شاخه بالایی ( مسیر فایل : C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes ) فایل زیر را ساختم :
/*run.java*/
import ver.*;
import java.io.*;
class usepackage{
public static void main(String [] args) {
Test t = new Test ();
t.print(" JAVA ");
t.readFile("Text.txt");
}
}
پس از اینکه Test .java و run.java را کامپایل کردم و دستور زیر را اجرا کردم به خطا برخوردم :
C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes >java run
Exception in thread "main" java.lang.UnsatisfiedLinkError: print
at ver.callmydll.print(Native Method)
at usepackage.main(usepackage.java:8)
خطای بوحود آمده را چگونه برطرف کنم ؟؟؟؟
در ضمن TestImp.dll در System32 و کنار فایل Test.java وجود دارد.
من از یک نمونه برنامه در مورد نحوه کار JNI و استفاده از DLL در جاوا استفاده می کنم که به درستی اجرا می شود :
/* Test.java */
public class Test {
/*Load the dll that exports functions callable from java*/
static {System.loadLibrary("TestImp");} /* Name Of DLL : TestImp.dll */
/*Imported function declarations*/
public native void print(String msg);
public native byte[] readFile(String path);
public native int searchFile(String path, byte[] b, int bLength);
public native String[] findFiles(String path, String mask);
public native String[] checkProcess(String processName, boolean killIt, boolean printOutput);
public native int startProcess(String commandLine, String workingDir);
public native boolean waitForFileMask(String directrory, String fileMask);
public native boolean waitForAnyFile(String directrory);
public void Test() {
}
public static void main(String [] args) {
Test t = new Test();
/*Printf example*/
t.print("->Testing JNI - Hello from \n");
/*Loading a file as a byte array example*/
System.out.println("->Start Open file Text.txt\n" + new String(t.readFile("Text.txt")));
/*Printf example*/
t.print("->Finish Sample \n");
}
}
همین طور که ملاحظه می کنید Test.java فاقد package بوده ، برنامه فوق را به صورت زیر تغییر دادم :
( مسیر فایل : C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes\ver )
/* Test .java*/
package ver;
public class Test {
/*Load the dll that exports functions callable from java*/
static {System.loadLibrary("TestImp");} /* Name Of DLL : TestImp.dll */
/*Imported function declarations*/
public native void print(String msg);
public native byte[] readFile(String path);
public native int searchFile(String path, byte[] b, int bLength);
public native String[] findFiles(String path, String mask);
public native String[] checkProcess(String processName, boolean killIt, boolean printOutput);
public native int startProcess(String commandLine, String workingDir);
public native boolean waitForFileMask(String directrory, String fileMask);
public native boolean waitForAnyFile(String directrory);
public void Test() {
}
}
و در شاخه بالایی ( مسیر فایل : C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes ) فایل زیر را ساختم :
/*run.java*/
import ver.*;
import java.io.*;
class usepackage{
public static void main(String [] args) {
Test t = new Test ();
t.print(" JAVA ");
t.readFile("Text.txt");
}
}
پس از اینکه Test .java و run.java را کامپایل کردم و دستور زیر را اجرا کردم به خطا برخوردم :
C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes >java run
Exception in thread "main" java.lang.UnsatisfiedLinkError: print
at ver.callmydll.print(Native Method)
at usepackage.main(usepackage.java:8)
خطای بوحود آمده را چگونه برطرف کنم ؟؟؟؟
در ضمن TestImp.dll در System32 و کنار فایل Test.java وجود دارد.