It's not a secret anymore!
The moose likes Other JSE/JEE APIs and the fly likes A JNI question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "A JNI question" Watch "A JNI question" New topic

A JNI question

Abdullah Javid

Joined: Jan 06, 2002
Posts: 12
This is Abdullah. I have been programming in Java for an year but I have not used Java JNI API uptill now. Now I need to call some Windows API(native) from withing Java code. Someone please tell me how can I do this. I think this could be done with JNI. I tried to make my hands dirty with JNI but I think it could only call methods from C/C++ classes which we have written. Will anybody please tell me how to call Windows APIs which Microsoft has implemented using Visual C++(or C++). Thanks in advance. I am humbly looking for your replies.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
If you can call your own native methods from Java then you probably know all you need to make this work already. The native method you want to call has to be available to the java code, and you do this with the loadLibrary() method, just as you do with your own library. In this case, you'd be passing the name of the windows DLL that has the API you are trying to call.
I'm not familiar with all the MFC DLLs so I don't know their names, but if you do Windows programming you probably have a way of finding out which DLL contains the methods you want to call. Then you just load that DLL with the loadLibrary() method, and you should be able to call the methods within it from your java app.

SCJP 1.4
Stu Glassman
Ranch Hand

Joined: Jul 01, 2002
Posts: 91
Rob, I just want to double-check on what you're saying. Can you actually call any native method (stored in a library) from Java, or can you only call methods that have been explicitly declared as Java callable?
edited - Sorry, there is no JNI section
[ July 19, 2002: Message edited by: Stu Glassman ]
Abdullah Javid

Joined: Jan 06, 2002
Posts: 12
Thanks Rob.
I am not fully satisfied with your reply. I think Java can only call those native mathods which are explicitly defined as they are callable by Java. Please tell me if I am wrong. I am looking forward for your reply. If anyone knows something in this regard, please tell.
I agree. Here's the link:
subject: A JNI question
It's not a secret anymore!