Java小題,通過JNI調用本地C++共用庫中的對應方法實現楊輝三角的繪製

來源:互聯網
上載者:User

標籤:檔案中   namespace   var   form   x86   void   配置環境   stream   引入   

 

1.在Eclipse中配置Javah,配置如下

位置是你javah.exe在你電腦磁碟上的路徑

位置:C:\Program Files\Java\jdk1.8.0_112\bin\javah.exe

工作目錄:${project_loc}/src

自變數:-classpath .;./classes -d  "${project_loc}/jni" -jni ${java_type_name}

 

2.建立一個java檔案

 1 package experience5; 2  3 public class NewNineToNine { 4     public native void showInfo(); 5     static{ 6         System.loadLibrary("sy5-1"); 7     } 8     public static void main(String[] args) { 9         // TODO Auto-generated method stub10         NewNineToNine obj = new NewNineToNine();11         obj.showInfo();12     }13 14 }15  

注意匯入動態連結程式庫不需要加尾碼名

將要用C++或C語言實現的方法設定成native類型

 

 3.選擇要通過javah產生的類

(需要點擊一下吧,不然會報錯:Launching javah has encountered a problem.The selected resourse does not resolve to a Java element

或者Launching javah has encountered a problem.Variable references empty selection :${project_loc}

只要點擊了就好,個人覺得是這樣哇》。《)

然後點擊運行

這樣就會在工程檔案夾裡產生一個jni的檔案夾:

在jni包裡面產生了“experience5_NewNineToNine.h”

 

4.編寫一個C++檔案,我用的是猥瑣死丟丟2013,選擇win32控制台程式,建立一個空白的 dll動態連結程式庫 

複製之前產生的“experience5_NewNineToNine.h“檔案,

和C:\Program Files (x86)\Java\jdk1.8.0_151\include目錄下的jni.h檔案

,以及C:\Program Files (x86)\Java\jdk1.8.0_151\include\win32目錄下的jni_md.h檔案放到和test.cpp同目錄下

=======================

“experience5_NewNineToNine.h”檔案:注意javah編譯出的標頭檔開啟之後顯示的是#include<jni.h>因為我們要引入jdk下的 jni.h檔案,所以要用雙引號

 

/* DO NOT EDIT THIS FILE - it is machine generated */#include "jni.h"/* Header for class experience5_NewNineToNine */#ifndef _Included_experience5_NewNineToNine#define _Included_experience5_NewNineToNine#ifdef __cplusplusextern "C" {#endif/* * Class:     experience5_NewNineToNine * Method:    showInfo * Signature: () */JNIEXPORT void JNICALL Java_experience5_NewNineToNine_showInfo  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
在源檔案中建立一個test.cpp
JNIEXPORT void JNICALL Java_experience5_NewNineToNine_showInfo (JNIEnv *, jobject);
就是在javah產生的標頭檔中聲明的方法,我們需要在test.cpp中把這個方法實現
這些代碼沒最佳化,只到了能用的程度

test.cpp:
 1 #include<iostream> 2 #include"experience5_NewNineToNine.h" 3 using namespace std; 4 JNIEXPORT void JNICALL Java_experience5_NewNineToNine_showInfo(JNIEnv *env, jobject obj) 5 { 6     int a[11][11]; 7     for (int i = 1; i <= 10; i++) { 8         a[i][1] = 1; 9         a[i][i] = 1;10         for (int j = 2; j<i; j++) {11             a[i][j] = a[i - 1][j - 1] + a[i - 1][j];12         }13 14         if (i>1) {15             for (int z = 0; z < (10 - i); z++) {16                 printf("  ");17 18             }19             for (int j = 1; j <= i; j++) {20                 cout << "   " << a[i][j];21             }22         }23         else    cout << "    " << a[1][1] << endl;24 25         cout << endl;26 27     }28 }

.要在java中輸出的內容就在test.cpp中的

 JNIEXPORT void JNICALL Java_experience5_NewNineToNine_showInfo(JNIEnv *env, jobject obj)方法體中實現
然後直接編譯運行過,會提示不能運行.dll檔案,那麼成功產生sy5-1.dll

 

==================
然後還沒完:
  讓java程式成功調用sy5-1.dll有兩種方法,自然有一種比較煩
    1.複製sy5-1.dll到系統的C:\Windows\System32目錄下,預設環境變數有這個
    2.配置環境變數指向動態連結程式庫所在的路徑(這種表達我也不知道對不對,意思到了就好0.0)

配置好了就不用一次一次複製了,

 


==================
最後一步:
    重啟eclipse,編譯運行。結果如下:


發生的問題:
  1.拋出了java.lang.UnsatisfiedLinkError異常: no sy1 in java.library.path,程式沒找到.dll檔案,放對位置就沒毛病了
  2.can‘t load IA 32-bit .dll on a AMD 64-bit platform錯誤,參考網上回答,最後我用了一個32位的JDK然後妥了;
    然後在StackOverflow裡有個回答:https://stackoverflow.com/questions/8113080/cant-load-ia-32-bit-dll-on-a-amd-64-bit-platform
    裡面是用cmd編譯運行,有點迷,所以乾脆下個32位JDK一了百了
  3.溜了溜了

 

 




 

 

Java小題,通過JNI調用本地C++共用庫中的對應方法實現楊輝三角的繪製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.