Java applet & native method (JNI) -- Snippets
来源:互联网 发布:java包依赖 编辑:程序博客网 时间:2024/05/22 06:08
1. Java applet
>javac -d . HelloApplet.java
>appletviewer HelloApplet.html
或者直接用IE打开
public class HelloApplet extends java.applet.Applet
{
public void paint(java.awt.Graphics g)
{
g.drawString("Hello Applet!", 50, 25);
}
public static void main(String[] args)
{
System.out.println("Hello" + " " + "World" + "!");
}
}
<html>
<head>
<title>Hello Applet</title>
</head>
<body>
Here is the "Hello Applet":
<applet code="HelloApplet.class" width="150" height="25"></applet>
</body>
</html>
2. native method
1) Java文件, HelloJni.java, 并编译
>javac -d . HelloJni.java
public class HelloJni
{
private static final String jniLib = "HelloJni";
public native int add(int i1, int i2);
static
{
System.loadLibrary(jniLib);
}
public static void main(String[] args)
{
int i1 = 100;
int i2 = 200;
System.out.println(new HelloJni().add(i1, i2));
}
}
2) 生成C++ (Native) 文件tony_test_HelloJni.h
>javah -jni tony.test.HelloJni
3) 编写HelloJni.cpp
JNIEXPORT jint JNICALL Java_tony_test_HelloJni_add(JNIEnv* env, jobject obj, jint i1, jint i2)
{
int i = i1 + i2;
return i;
}
4) 编译生成dll
Windows:
>cl -I%JAVA_HOME%/include -I%JAVA_HOME%/include/win32 -LD HelloJni.cpp -FeHelloJni.dll
Solaris:
#cc -G -I/usr/local/java/include -I/usr/local/java/include/solaris HelloJni.cpp / -o libHelloJni.so
-G选项指生成动态链接库.
这里需要说明的是, 在windows下和solaris动态链接库的名称分别为HelloJni.dll和libHelloJni.so.
在java.lang.System下有三个与加载动态链接库有关的函数: load, mapLibraryName, loadLibrary.
load参数为动态链接库绝对路径, 如System.load("F://codes//jnicode//HelloJni.dll");
mapLibraryName会根据操作系统和参数动态链接库名来返回对应的动态链接库文件名, 比如, System.mapLibraryName("HelloJni"); 在Windows下返回HelloJni.dll, 在Solaris下返回libHelloJni.so;
loadLibrary参数为动态链接库名, 如System.load("HelloJni"); 对于不同的系统它会加载对应的库(windows下加载HelloJni.dll, Solaris下加载libHelloJni.so), 而不需改Java代码.
5) 运行Java程序
>java tony.test.HelloJni
两个不错的参考:
Jni中C++和Java的参数传递
[JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
推荐一个开源项目: Jace. 极大地方便JNI编程.
http://sourceforge.net/projects/jace/
jace.1.1rc1_05
"Jace is a toolkit designed to make it easy to write JNI-based programs. Jace consists of a C++ runtime library, and a set of tools written in Java. Because Jace is not a framework, it leaves all possible options open to the developer.
Jace's C++ runtime library provides an easy to use API that allows user to manipulate Java objects as if they were C++ objects. Behind the scenes, the runtime library automatically manages the lifetimes of Java references, the mapping of Java exceptions to C++ exceptions, and the binding of C++ threads to the JVM.
Jace's Java tools can automatically generate C++ Proxy classes and C++ Peer classes from Java classes. This means that you can easily access any Java object or implement any Java native function from your C++ code. "
- Java applet & native method (JNI) -- Snippets
- Java Native Method JNI理解
- JNI(Java Native Interface) Native Method
- JNI 与 native method
- JNI (JAVA NATIVE METHOD)JAVA和其他程序交互
- java applet调用JNI
- [JNI] How to get the java's native method body?
- [JNI] java.lang.UnsatisfiedLinkError:Native method not found && no implementation found for native ~
- Android之JNI错误:no implementation found in native -java.lang.UnsatisfiedLinkError: Native method not
- java Native Method初涉
- java Native Method初涉
- java Native Method初涉
- java Native Method
- java Native Method初涉
- java Native Method初涉
- java Native Method初涉
- java native method
- Java Native Method introduction
- 成功的十个好习惯
- ACM竞赛要掌握的知识
- asp.net笔试题收藏
- 每个开发人员现在应该下载的十种必备工具
- DataGrid技巧
- Java applet & native method (JNI) -- Snippets
- asp.net--IE好玩的玩意
- 漫谈创业和管理-程序员5大思维障碍
- 忙碌白领10种危险生活方式
- struts2 :Error building results for action login in namespace
- 4月
- 第一次编写EJB总结!
- GBK字符集知识
- java 五年经典文章回顾