Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling DLL from java file !!!

 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I call Dll from an java file ?
This is the code I am using but getting error in it.
class MyAdd
{
static
{
System.loadLibrary("PrjAdd");
System.out.println("Loading Lib");
}

public native int AddNum(int i,int j);
public static void main(String [] args)
{
MyAdd nt = new MyAdd();
System.out.println("before calling Addnum method");
nt.AddNum(2,3);
System.out.println("After calling Addnum method");
}
}
This is the runtime error:
ERROR : Exception in thread "main" java.lang.UnsatisfiedLinkError: AddNum
at MyAdd.main(MyAdd.java:15)

Pls help me out.
Have a great day.
Sandeep
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not make a direct call to a C library from Sun JDK. ( I think MS JDK allows it). You need to use JNI. Check the JNI doucmentaion for details.
HTH
Sanjay
 
Sandeep Lodhia
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for ur response.
HOwever, I am making an VB call as the dll is written in VB.
Moreover,I have got the above script of code from Sun's forum.
SO I guess, it should work fine.
Pls let me know what exactly is needed to call a VB function from java file.
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you took from sun's forum is right. But that is not all that you need to call a DLL's method. You need to have the implementations for the native methods u have declared

After compiling this file, you need to create the Headers for this using the javah utility. It will give you a .h file. you need to write implementation for that. This implementation code will have the calls to ur DLL methods.
 
Screaming fools! It's nothing more than a tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!