Results 1 to 3 of 3
  1. #1
    smh
    smh is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Question Using DLL in Java

    I have a working sample program on how to use DLL in java with JNI. I use to be properly implemented:
    Java Code:
    /* 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");
           		 }	
    }
    So you see that the package was not Test.java, these programs have changed as follows:
    (file path : C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes\ver)
    Java Code:
    /* 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() {
       		 }
    }
    And upper branches (file path : C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes) made the following file:
    Java Code:
    /*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");
    	}
    }
    After the Test. Java & run.java compile and run the following command and I got across the error:
    Java Code:
    C:\Tomcat5.5\webapps\ROOT\JNI\WEB-INF\classes >java run
    Java Code:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: print
            at ver.callmydll.print(Native Method)
            at usepackage.main(usepackage.java:8)
    TestImp.dll Test.java file in System32 and the next there.
    How do you fix the error ???
    Email : <mod edit: deleted -- let's keep all discussions public>
    Last edited by Fubarable; 11-24-2010 at 12:54 PM. Reason: see Mod edit above.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by smh View Post
    How do you fix the error ???
    Email : <mod edit: deleted>
    I won't email you; you write here you read here; if you change the package of your class the native code names also change. Your print() method has a different name now; you should change those names in your C/C++ code and recompile everything. (for the 'new' names read the generated .h file).

    kind regards,

    Jos
    Last edited by Fubarable; 11-24-2010 at 12:55 PM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •