当前位置: 技术问答>java相关
请问在JAVA中有没有提供对C或C++接口的访问
来源: 互联网 发布时间:2015-03-25
本文导语: 在JAVA中能不能调用C或C++的API 或程序,谢谢 | package app1; class ShowMsgBox{ public static void main(String[] args) { ShowMsgBox app = new ShowMsgBox(); app.ShowMessage("Generated with JNI"); } private native...
在JAVA中能不能调用C或C++的API 或程序,谢谢
|
package app1;
class ShowMsgBox{
public static void main(String[] args) {
ShowMsgBox app = new ShowMsgBox();
app.ShowMessage("Generated with JNI");
}
private native void ShowMessage(String msg); //declare native method
static{
System.loadLibrary("MsgImpl");
}
}
之后:javac ShowMessage.java;javah -jni app1.ShowMessage(生成app1_ShowMsgBox.h)
最后实现dll:
#include
#include "app1_ShowMsgBox.h"
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD dwReason, void** lpReserved) {
return TRUE;
}
JNIEXPORT void JNICALL
Java_ShowMsgBox_ShowMessage(JNIEnv * jEnv,
jobject this, jstring jMsg) {
const char * msg;
msg = (*jEnv)->GetStringUTFChars(jEnv, jMsg,0);
MessageBox(HWND_DESKTOP, msg,"Thinking in Java: JNI",MB_OK |MB_ICONEXCLAMATION);
(*jEnv)->ReleaseStringUTFChars(jEnv, jMsg,msg);
}
(摘自thinking in java)
class ShowMsgBox{
public static void main(String[] args) {
ShowMsgBox app = new ShowMsgBox();
app.ShowMessage("Generated with JNI");
}
private native void ShowMessage(String msg); //declare native method
static{
System.loadLibrary("MsgImpl");
}
}
之后:javac ShowMessage.java;javah -jni app1.ShowMessage(生成app1_ShowMsgBox.h)
最后实现dll:
#include
#include "app1_ShowMsgBox.h"
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD dwReason, void** lpReserved) {
return TRUE;
}
JNIEXPORT void JNICALL
Java_ShowMsgBox_ShowMessage(JNIEnv * jEnv,
jobject this, jstring jMsg) {
const char * msg;
msg = (*jEnv)->GetStringUTFChars(jEnv, jMsg,0);
MessageBox(HWND_DESKTOP, msg,"Thinking in Java: JNI",MB_OK |MB_ICONEXCLAMATION);
(*jEnv)->ReleaseStringUTFChars(jEnv, jMsg,msg);
}
(摘自thinking in java)