Solved

on java native method?

Posted on 2001-06-22
38
578 Views
Last Modified: 2013-11-23
/*
 Hi all,
   I want to use a windows native method which in advapi32.dll, the founction is GetUserName(String s, long n),
  But it alway got error:

  The result is:
java.lang.UnsatisfiedLinkError: GetUserName
     at untitled25.Frame1.GetUserName(Native Method)
     at untitled25.Frame1.<init>(Frame1.java:15)
     at untitled25.Frame1.main(Frame1.java:22)
    Can anyone tell me the whys? who can help me to make it?
    Best whishes,

*/

package untitled25;

import java.awt.*;
import javax.swing.JFrame;

public class Frame1 extends JFrame {
  static {
      System.loadLibrary("advapi32");


  }
  public native long GetUserNameW(String s,long n);
  public Frame1() {
    String s="   ";
    long k=  GetUserNameW(s,255l);
    System.out.println( GetUserNameW(s,255l));
    System.out.println(s);

  }

  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
     java.awt.Image im=Toolkit.getDefaultToolkit().getImage("e:\\me.jpg");

    frame1.setIconImage(im);
    frame1.setSize(300,300);
    frame1.setVisible(true);

  }
}
/*
  The result is:
java.lang.UnsatisfiedLinkError: GetUserName
     at untitled25.Frame1.GetUserName(Native Method)
     at untitled25.Frame1.<init>(Frame1.java:15)
     at untitled25.Frame1.main(Frame1.java:22)
        */
0
Comment
Question by:liluqun
  • 25
  • 4
  • 2
  • +6
38 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 6217378
u can't directly call the dll from java i think u need a bridge to invoke a methods from dll
try refering
www.linar.com
cheers
RJ
0
 
LVL 1

Expert Comment

by:tonus
ID: 6217746
Try giving full file name

ie.,System.loadLibrary("advapi32.dll");


 
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6217843
Hi lilugun

Why can't u use System Property to know the current user name

Why do u need a native libraries to do that


Shyam
0
 
LVL 3

Expert Comment

by:falter
ID: 6218249
Follow the comment from rjackman, you need a wrapper do access existing DLL's.
Try javah -jni on your class you will get a C-header file
and see the signature (Name of the native method) searched from the JVM.
Write a native method with the name you got from the header taking the input parms from Java translate them to C parms.
Call the GetUserName(...) method and return the translated result to the JVM.

Create your own dll with a C compiler .
Load this library instead of advapi32.dll
Ensure both dll's the new one and advapi32.dll are in the PATH and run your Java application.

More information and examples you will find for example at
http://java.sun.com/docs/books/tutorial/native1.1/index.html

cheers
jf
0
 

Author Comment

by:liluqun
ID: 6220735
I mean if I got a *.dll file, and I know it contains some functions, how can I use the fountions aa a native method?
0
 

Expert Comment

by:ImrML
ID: 6222664
Hi lilugun,

I beleive I am very near to solve the problem. The theory says the native methods should be ended with semicolon in the body of the method.
Now in your case the method acts like abstract without implementation. So everything is fine just change your native methods put the semicolon into the block or body of the method.
Try this as I don't have Jdk with me to try at the moment.

Thanks
ImrML
0
 

Expert Comment

by:ImrML
ID: 6227162
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227163
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227164
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227165
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227166
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227167
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227168
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227169
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227170
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227171
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227172
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227173
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227174
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:ImrML
ID: 6227175
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227176
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227177
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227178
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227179
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227180
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227181
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227182
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227183
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227184
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 

Expert Comment

by:ImrML
ID: 6227185
Lilugun,
What you are writing may be correct as my suggestion doesn't work. The exception raised may be due to the type mismatch.

Sorry, for wrong suggestions.

Thanks
Imrml
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6227208
Mr ImrML
What happened
Why do u post so many messages

Shyam
0
 

Author Comment

by:liluqun
ID: 6227317
Please me useful advice!
I mean if I got a *.dll file, and I know it contains some functions, how can I use the fountions as
a native method?
 IN VISUAL BASIC we know we can use any *.DLL if we know the function we can use it freely!
 IN JAVA ,can we made it?
0
 
LVL 3

Expert Comment

by:falter
ID: 6227472
liluqun,

as rjackman said before, you need a wrapper-DLL.

Why?
public class Frame1 extends JFrame {
 
 // the native method
 public native long GetUserNameW(String s,long n);
 // some more code for the class
 ...
 }

If you run you will get the name of the method in your native dll in your example:
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv *, jobject, jstring, jlong);

I'm sure there will be no method inside advapi32.dll with
this name.

you need a piece of C-code which will do the mapping:

#include <Frame1.h> // output from cmd  javah -jni Frame1
// the wrapper method
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv * env, jobject obj, jstring jstr , jlong myLong)
{
 char buf[1024]; // buffer for converting jstring to C
 long retValue = -1;
 const char *str = (*env)->GetStringUTFChars(env,jstr,0);
 retValue = GetUserName(str, myLong);
 // releasing the str/jstr pair
 (*env)->ReleaseStringUTFChars(env,jstr,str);
 return retValue;
}

Compile and Link this file and load the resulting DLL.

Hope this helps
jf
0
 
LVL 3

Expert Comment

by:falter
ID: 6227502
liluqun,

as rjackman said before, you need a wrapper-DLL.

Why?
public class Frame1 extends JFrame {
 
 // the native method
 public native long GetUserNameW(String s,long n);
 // some more code for the class
 ...
 }

If you run you will get the name of the method in your native dll in your example:
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv *, jobject, jstring, jlong);

I'm sure there will be no method inside advapi32.dll with
this name.

you need a piece of C-code which will do the mapping:

#include <Frame1.h> // output from cmd  javah -jni Frame1
// the wrapper method
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv * env, jobject obj, jstring jstr , jlong myLong)
{
 char buf[1024]; // buffer for converting jstring to C
 long retValue = -1;
 const char *str = (*env)->GetStringUTFChars(env,jstr,0);
 retValue = GetUserName(str, myLong);
 // releasing the str/jstr pair
 (*env)->ReleaseStringUTFChars(env,jstr,str);
 return retValue;
}

Compile and Link this file and load the resulting DLL.

Hope this helps
jf
0
 
LVL 3

Expert Comment

by:falter
ID: 6227505
liluqun,

as rjackman said before, you need a wrapper-DLL.

Why?
public class Frame1 extends JFrame {
 
 // the native method
 public native long GetUserNameW(String s,long n);
 // some more code for the class
 ...
 }

If you run you will get the name of the method in your native dll in your example:
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv *, jobject, jstring, jlong);

I'm sure there will be no method inside advapi32.dll with
this name.

you need a piece of C-code which will do the mapping:

#include <Frame1.h> // output from cmd  javah -jni Frame1
// the wrapper method
JNIEXPORT jlong JNICALL Java_Frame1_GetUserName   (JNIEnv * env, jobject obj, jstring jstr , jlong myLong)
{
 char buf[1024]; // buffer for converting jstring to C
 long retValue = -1;
 const char *str = (*env)->GetStringUTFChars(env,jstr,0);
 retValue = GetUserName(str, myLong);
 // releasing the str/jstr pair
 (*env)->ReleaseStringUTFChars(env,jstr,str);
 return retValue;
}

Compile and Link this file and load the resulting DLL.

Hope this helps
jf
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6908029
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.12050000.html
http://www.experts-exchange.com/questions/Q.20015585.html
http://www.experts-exchange.com/questions/Q.20074385.html
http://www.experts-exchange.com/questions/Q.20074388.html
http://www.experts-exchange.com/questions/Q.20074386.html
http://www.experts-exchange.com/questions/Q.20074390.html
http://www.experts-exchange.com/questions/Q.20139915.html
http://www.experts-exchange.com/questions/Q.20139916.html
http://www.experts-exchange.com/questions/Q.20139913.html
http://www.experts-exchange.com/questions/Q.20159531.html
http://www.experts-exchange.com/questions/Q.20165862.html
http://www.experts-exchange.com/questions/Q.20190596.html
http://www.experts-exchange.com/questions/Q.20244233.html
http://www.experts-exchange.com/questions/Q.20254620.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 5

Expert Comment

by:vemul
ID: 7621013
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- To be PAQ'ed and points refunded
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 7643096
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now