020-29133788
    资 讯
    您的位置:首页 >> 资 讯 >> 软件应用 >> 编程开发 >> 正文
    利用c++呼叫java的物件

    点击:   发布日期:2013-01-25

    本文来自 www.020fix.com

    利用gnu g++可以呼叫java的物件
    /**
    1:http://sourceforge.net/forum/forum.php?forum_id=500176
    下戴MinGW 5.0.0 之后安装,选取安装g++程gcj
    2:http://gnuwin32.sourceforge.net/packages/libiconv.htm
    下戴Binaries和Developer files,之后解压缩放在和mingw同的个目录
    lib和bn的档案都贴过去 ,设路径在dos下打:set path=c:\mingw\bin
    1
    compiler c++
    g++ -c source.cpp
    g++ -o source.exe source.o

    2java转exe

    gcj -C source.java //把产生 class档
    gcjh source //产生source.h (interface)
    gcj -c source.java //把java compile 成 obj //(implement)
    gcj --main=source -o source source.o 把 java obj 变成 exe

    3 c++使用java api (使用cni语法)
    g++ -c source.cpp or gcj -c source.cpp
    gcj -o source.exe source.o -lgcj -lstdc++

    4内嵌assembly language
    using
    __asm("your code/n/t")//online one line
    or
    __asm volatile (
    "code" /n/t
    "code" /n/t
    : : :) //extend model
    **/
    注意的是c++只能呼叫java的物件,c++类别不能继承java类别
    范例
    /** MyThread.java **/
    public class MyThread extends Thread {
      public void start() {
       run();
      }
      public void run() {
        System.out.println("function call from java");
      }
    在dos下打:
    gcj -C MyThread.java
    gcj -c MyThread.java
    gcjh MyThread
    之后会产生 MyThread.class,MyThread.o.MyThread.h三个档

    /**Test.cpp **/
    #include <gcj/cni.h>
    #include "MyThread.h"
    #include <iostream>
    using namespace std;

    int main(int argc, char *argv) {
      JvCreateJavaVM(NULL);
      JvAttachCurrentThread(NULL, NULL);
      MyThread* t=new MyThread();
      t->start();
      delete t;
      cout<<"hello from C++";
      JvDetachCurrentThread();
    }
    }
    在dos下打:
    g++ -c Test.cpp
    gcj -o Test.exe Test.o MyThread.o -lgcj -lstdc++
    之后产生Test.exe档
    执行Test.exe
    印出
    function call from java
    hello from C++

    如何传送与接收参数

    C++ function传参数给java method和接收java method的参数

    /** JavaSource.java **/
    public class JavaSource{

      public int getInt() {
        return 100;
      }

      public double getDouble() {
        return 3.14159;
      }

      public  byte[] getString() {
        byte[] b=null;
        try {
          String word="begin 123 abc棕熊坦克碁许\功\盖\ end";
          b=word.getBytes("iso-8859-1");
        }
        catch(Exception e) {}
        return b;
      }
      public  byte[][] getStrings() {
        byte[][] b=null;
        try {
          String[] words={"abcd","1234","987654"};
          b=new byte[3][];
          b[0]=words[0].getBytes();
          b[1]=words[1].getBytes();
          b[2]=words[2].getBytes();
        }
        catch(Exception e) {}
        return b;
      }

      public  int[] getInts_1() {
        return new int[]{9,8,7,6,5,4,3,2,1,0};
      }

      public int[][] getInts_2() {
        return new int[][] {
          {10,11,12,-1},{21,22,23,24,25,-1}
        };
      }

      public void printInt(int i) {
        System.out.println("int i="+i+" from c++");
      }

      public void printInt_1(int[] i) {
        System.out.println("int[] {");
        for(int j=0;j<i.length;j++)
        System.out.println("("+i[j]+")");
        System.out.println("} from c++");
      }
      public void printInt_2(int[][] i) {
       System.out.println("int[][] { ");
        for(int j=0;j<i.length;j++)
          printInt_1(i[j]);
        System.out.println("} from c++");
      }
      public void printString_1(byte[] word) {
        System.out.println("char[] word="+new String(word)+" from c++");
      }

      public void printString_2(byte[][] word) {
        System.out.println("char[][] { ");
        for(int i=0;i<word.length;i++)
          printString_1(word);
        System.out.println("}");
      }
    }

    command:在dos下打
    gcj -c JavaSource.java  --encoding=iso-8859-1
    gcj -C JavaSource.java  --encoding=iso-8859-1
    gcjh JavaSource
    之后产生JavaSource.class,JavaSource.o,JavaSource.h

    /** test.cpp **/
    #include <gcj/cni.h>
    #include "JavaSource.h"
    #include <iostream>
    using namespace std;

    namespace Control_Java {

      class CObject {
        char* word;
        char** words;
        int* p_1;
        int** p_2;
        JavaSource* s;
        public:

          CObject() {
            s=new JavaSource();
          }
          ~CObject() {
            delete [] word;
            delete [] *(words+0);
            delete [] *(words+1);
            delete [] *(words+2);
            delete [] words;
            delete [] p_1;
            delete [] *(p_2+0);
            delete [] *(p_2+1);
            delete [] p_2;
            delete s;
          }
          int getInt() {
            return s->getInt();
          }
          double getDouble() {
            return s->getDouble();
          }


          char* getString() {
            jbyteArray array=s->getString();
            jbyte* jp=elements(array);
            int length=(int)JvGetArrayLength(array);
            word=new char[length];
            int p=0;
            for(int p=0;p<length;p++)
              *(word+p)=*(jp+p);
            word[length]=0;
            return word;
          }
          char** getStrings() {
            
            JArray<jbyteArray>* array=s->getStrings();
            int length=(int)JvGetArrayLength(array);
            jbyteArray* array_1=elements(array);
            jbyteArray tints[length];
            words=new char*[length];
            for(int p=0;p<length;p++) {
              *(tints+p)=*(array_1+p);
              jbyte* p_i=elements(tints[p]);
              int length_1=(int)JvGetArrayLength(tints[p]);
              *(words+p)=new char [length_1];
              int index=0;
              for(index=0;index<length_1;index++)
                *(*(words+p)+index)=*(p_i+index);
              *(*(words+p)+index)=0;
              }
            return words;  
          }
          int* getInt_1() {
            jintArray array=s->getInts_1();
            jint* jp=elements(array);
            int length=(int)JvGetArrayLength(array);
            p_1=new int[length];
            for(int p=0;p<length;p++)
              *(p_1+p)=*(jp+p);
            return p_1;
          }

          int** getInt_2() {
            
            JArray<jintArray>* array=s->getInts_2();
            int length=(int)JvGetArrayLength(array);
            jintArray* array_1=elements(array);
            jintArray tints[length];
            p_2=new int*[length];
            for(int p=0;p<length;p++) {
              *(tints+p)=*(array_1+p);
              jint* p_i=elements(tints[p]);
              int length_1=(int)JvGetArrayLength(tints[p]);
              *(p_2+p)=new int [length_1];
              int index=0;
              for(index=0;index<length_1;index++)
                *(*(p_2+p)+index)=*(p_i+index);
              }
            return p_2;  
          }

          void printInt(int i) {
            s->printInt(i);
          }
          void printInt_1(int word[],int length) {
            jintArray array=JvNewIntArray(length);
            jint* p=elements(array);
            for(int i=0;i<length;i++)
            *(p+i)=*(word+i);
            s->printInt_1(array);
          }
          void printInt_2(int* words[],int length[]) {
            JArray<jintArray>* array=(JArray<jintArray>*)JvNewObjectArray(length[0],JvNewIntArray(0)->getClass(),NULL);
            jintArray* p=elements(array);
            int i=0;
            for(i=0;i<length[0];i++) {
              int length_1=length[i+1];
              *(p+i)=JvNewIntArray(length_1);
              jint* p_1=elements(*(p+i));
              for(int j=0;j<length_1;j++)
                *(p_1+j)=*(words+j);
            }
            s->printInt_2(array);   
          }
          void printString_1(char word[],int length) {
            jbyteArray array=JvNewByteArray(length);
            jbyte* p=elements(array);
            for(int i=0;i<length;i++)
            *(p+i)=*(word+i);
            s->printString_1(array);
          }

          void printString_2(char* words[],int length) {
            JArray<jbyteArray>* array=(JArray<jbyteArray>*)JvNewObjectArray(length,JvNewByteArray(0)->getClass(),NULL);
            jbyteArray* p=elements(array);
            int i=0;
            for(i=0;i<length;i++) {
              int length_1=0;
              while(*(words)!=0) {
                length_1++;
                words++;
              }
              int length_2=length_1;
              while(length_2>0) {
                length_2--;
                words--;
              }
              *(p+i)=JvNewByteArray(length_1+1);
              jbyte* p_1=elements(*(p+i));
              for(int j=0;j<length_1+1;j++)
                *(p_1+j)=*(words+j);
            }
            s->printString_2(array);   
          }
      };

    }
    namespace Without_Java {
      /**
      other class obj...
      **/
    }
    int main(int argc, char *argv) {
       cout<<"program begin"<<endl;
       {using namespace Control_Java;
         JvCreateJavaVM(NULL);
         JvAttachCurrentThread(NULL, NULL);
         int da1[]={1,2,3,4,5,6};
         int da2[]={10,20,30,40,50,60,70};
         int* das[2];
         int dalength[]={sizeof(das)/sizeof(int*),sizeof(da1)/sizeof(int),sizeof(da2)/sizeof(int)};
         das[0]=da1;
         das[1]=da2;
         char word[]="begin *123abc棕熊坦克碁许\功\盖\) end";
         char* words[]={
          "message:",
          "汽车电脑(car pc)才是未来电脑科技的主流",
          "会windos,linux这都没什么了不起,懂车又懂电脑的人才酷",
          "欢迎有兴趣的人一起加入研究,更改车子内部电脑程式"
         };
         CObject obj=CObject();
       
         cout<<obj.getInt()<<" from java "<<endl;
         cout<<obj.getDouble()<<" from java "<<endl;
         cout<<obj.getString()<<" from java "<<endl;
         char** wo=obj.getStrings();
         cout<<"words from java:"<<*wo<<"\t"<<*(wo+1)<<"\t"<<*(wo+2)<<endl;
         int* p1=obj.getInt_1();
         cout<<"int[] {";
         for(int i=0;i<10;i++) cout<<'('<<*(p1+i)<<')';
         cout<<" }\n";
       
         int** p2=obj.getInt_2();
         cout<<"int[][] { "<<endl;

         cout<<"int[] {";
         for(int i=0;i<4;i++) cout<<'('<<*(*(p2+0)+i)<<')';
         cout<<"}"<<endl;
       
         cout<<"int[] {";
         for(int i=0;i<6;i++) cout<<'('<<*(*(p2+1)+i)<<')';
         cout<<"}"<<endl;
         cout<<"} from java \n";

         obj.printInt(1000);
         
         obj.printInt_1(da1,sizeof(da1)/sizeof(int));
         obj.printInt_2(das,dalength);
         obj.printString_1(word,sizeof(word)/sizeof(char));
         obj.printString_2(words,sizeof(words)/sizeof(char*));
         
        JvDetachCurrentThread();
      }
     
      {using namespace Without_Java;
        cout<<"hi,here don't need using java"<<endl;
      }
      cout<<"program end";
      return 0;
    }

    command :在dos下打
    g++ -c test.cpp
    gcj -o test.exe test1.o  JavaSource.o -lgcj -lstdc++