Wiki code

来自ling
跳转至: 导航搜索

示例

===HelloWorld===make.sh
#!/bin/sh# openbsd 4.9# gcc 4.2.1# openjdk 1.7.0export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.javac HelloWorld.javajavah HelloWorldgcc -shared libHelloWorld.c -o libHelloWorld.sojava HelloWorld
build.bat
:: Microsoft Visual Studio 2012 Visual C++ compilerSET VC="C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC":: Microsoft Windows SDK for Windows 7 and .NET Framework 4 SET MSDK="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A":: Java 1.7.0 update 21SET JAVA_HOME="C:\Program Files (x86)\Java\jdk1.7.0_21"call %VC%\vcvarsall.batjavac HelloWorld.javajavah HelloWorld%VC%\bin\cl /I%JAVA_HOME%\include /I%JAVA_HOME%\include\win32 /I%VC%\include /I%VC%\lib /I%MSDK%\Lib libHelloWorld.c /FelibHelloWorld.dll /LDjava HelloWorld
HelloWorld.java
class HelloWorld{	private native void print();	public static void main(String[] args)	{		new HelloWorld().print();	}	static{		System.loadLibrary("HelloWorld");	}}
HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class HelloWorld */#ifndef _Included_HelloWorld#define _Included_HelloWorld#ifdef __cplusplusextern "C" {#endif/* * Class:     HelloWorld * Method:    print * Signature: ()V */JNIEXPORT void JNICALL Java_HelloWorld_print  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
libHelloWorld.c
 #include <stdio.h> #include "HelloWorld.h" JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) {     printf("Hello World!\n");     return; }
Invocation:
$ chmod +x make.sh$ ./make.sh