Java和C語言混合編程--Windows版

整個工程下須要的文件:java

先來寫一下Java的類代碼:Sample1.java 點擊(此處)摺疊或打開 public class Sample1{ public native int intMethod(int n); public native boolean booleanMethod(boolean bool); public native String stringMethod(String text); public native int intArrayMethod(int[] intArray);spa

public static void main(String[] args)
{
    System.loadLibrary("SampleC"); //DLL庫的名字(C語言Windows XP下)
    Sample1 sample= new Sample1();
    
    int square = sample.intMethod(5);
    boolean bool = sample.booleanMethod(true);
    String text = sample.stringMethod("java");
    int sum = sample.intArrayMethod(new int[]{1,2,3,4,5,6,7,8,13});
    
    System.out.println("intMethod: " + square);
    System.out.println("booleanMethod:" + bool);
    System.out.println("stringMethod:" +text);
    System.out.println("intArrayMethod:" + sum);
    
}

} 而後編譯Sample1.java 提供一個bat文件編譯:JavaMakeUp.batcode

點擊(此處)摺疊或打開 set JAVABIN=D:\ProgramJava\Java\jdk1.6.0_30\bin set classpath=.;D:\ProgramJava\Java\jdk1.6.0_30\lib教程

set PATH=%JAVABIN% javac -classpath -Xlint:unchecked -verbose *.java pause 將 JAVABIN 和classpath 設置爲大家本身的Java路徑。 而後雙擊 JavaMakeUp.bat: 就能夠編譯出Sample1.class。 Java還有一個強大功能就是根據.calss生成C和C++使用的.h文件文件: 指令以下:get

javah Sample1 生成的.h文件以下:Sample1.h博客

點擊(此處)摺疊或打開 /* DO NOT EDIT THIS FILE - it is machine generated / #include <jni.h> / Header for class Sample1 */string

#ifndef _Included_Sample1 #define _Included_Sample1 #ifdef __cplusplus extern "C" { #endif /*it

  • Class: Sample1
  • Method: intMethod
  • Signature: (I)I */ JNIEXPORT jint JNICALL Java_Sample1_intMethod (JNIEnv *, jobject, jint);

/*io

  • Class: Sample1
  • Method: booleanMethod
  • Signature: (Z)Z */ JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod (JNIEnv *, jobject, jboolean);

/*編譯

  • Class: Sample1
  • Method: stringMethod
  • Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_Sample1_stringMethod (JNIEnv *, jobject, jstring);

/*

  • Class: Sample1
  • Method: intArrayMethod
  • Signature: ([I)I */ JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod (JNIEnv *, jobject, jintArray);

#ifdef __cplusplus } #endif #endif 請你們支持一下個人 商城: www.yangcheng520.com 謝謝了。 下面就是寫C語言代碼了: 使用VC++6.0編寫和編譯: SampleC.cpp: 點擊(此處)摺疊或打開 #include <stdio.h> #include <string.h> #include "Sample1.h"

#if 1

/*

  • Class: Sample1
  • Method: intMethod
  • Signature: (I)I */ JNIEXPORT jint JNICALL Java_Sample1_intMethod (JNIEnv env, jobject obj, jint num) { return numnum; }

/*

  • Class: Sample1
  • Method: booleanMethod
  • Signature: (Z)Z */ JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod (JNIEnv *env, jobject obj, jboolean boolean) { return !boolean; }

/*

  • Class: Sample1
  • Method: stringMethod
  • Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_Sample1_stringMethod (JNIEnv *env, jobject obj, jstring string) { const char *str=env->GetStringUTFChars(string, 0); char cap[128]; strcpy(cap, str); env->ReleaseStringUTFChars(string, 0); return env->NewStringUTF(strupr(cap)); }

/*

  • Class: Sample1
  • Method: intArrayMethod
  • Signature: ([I)I */ JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod (JNIEnv *env, jobject obj, jintArray array) { int i, sum=0; jsize len = env->GetArrayLength(array); jint *body=env->GetIntArrayElements(array, 0); for(i=0; i < len; i++) { sum += body[i]; } env->ReleaseIntArrayElements(array, body, 0); return sum; }

#endif 編譯也能夠使用bat文件編譯爲DLL庫: VCMakeUp.bat: 點擊(此處)摺疊或打開 set VCBIN=D:\Microsoft Visual Studio\VC98\Bin set INCLUDE=D:\Microsoft Visual Studio\VC98\Include set LIB=D:\Microsoft Visual Studio\VC98\Lib set JAVA_HOME=D:\ProgramJava\Java\jdk1.6.0_30

set PATH=%VCBIN%

cl /I%JAVA_HOME%\include /I%JAVA_HOME%\include\win32 /c *.cpp cl /LD /I$%JAVA_HOME%\include /I%JAVA_HOME%\include\win32 *.obj /FeSampleC.DLL VC++6.0路徑修改成你本身的 電腦路徑. 編譯出DLL運行既可。 運行指令:

java Sample1 結果以下:

D:\NewLandWork_LANPENG\Lan_POS_LIB\Java-C>java Sample1 intMethod: 25 booleanMethod:false stringMethod:JAVA intArrayMethod:49 你的博客 java教程 http://zencart.me

相關文章
相關標籤/搜索