Archive for the ‘GCJ’ Category

  1. Hello, Win32 API(CNI) World!

    Posted on 3月 26th, 2013 by cx20

    Win32 API(CNI)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は、GCJ にて CNI を利用した Win32 API の呼出し例となっている。

    ソースコード(Java)

    public class Hello {
        public static native int messageBox( int hwnd, String text, String caption, int type );
     
        public static void main( String[] args ) {
            messageBox( 0, "Hello, Win32 API World!

    クラスファイル作成

    C:¥> gcj -C Hello.java

    C++ ヘッダファイル作成

    C:¥> gcjh Hello.class

    ソースコード(C++ ヘッダファイル)

    // DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
     
    #ifndef __Hello__
    #define __Hello__
     
    #pragma interface
     
    #include <java/lang/Object.h>
    #include <gcj/array.h>
     
    extern "Java"
    {
        class Hello;
    }
     
    class Hello : public ::java::lang::Object
    {
     
    public:
      Hello();
      static jint messageBox(jint, ::java::lang::String *, ::java::lang::String *, jint);
      static void main(JArray< ::java::lang::String * > *);
      static ::java::lang::Class class$;
    };
     
    #endif // __Hello__

    ソースコード(C++ 実装ファイル)

    #include <gcj/cni.h>
    #include <windows.h>
    #include "Hello.h"
     
    jint Hello::messageBox(jint hwnd, ::java::lang::String* text, ::java::lang::String* caption, jint type)
    {
        jchar* _text = JvGetStringChars( text );
        jchar* _caption = JvGetStringChars( caption );
        int result = MessageBoxW( (HWND)hwnd, (LPCWSTR)_text, (LPCWSTR)_caption, (UINT)type );
        return result;
    }

    コンパイル方法

    C:¥> gcj -c Hello.cpp
    C:¥> gcj --main=Hello -o Hello Hello.o Hello.class

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API World!
    ---------------------------
    OK   
    ---------------------------
  2. Hello, GCJ World!

    Posted on 3月 25th, 2013 by cx20

    GCJ

    GCJ は GNU Compiler for the Java の略で、GCC に含まれるコンパイラの1つである。
    Java のライブラリが使用できるほか、CNI(Compiled Native Interface)を用いることで、C++ で作成したライブラリを呼び出すことが可能となっている。

    ソースコード(Java)

    public class Hello {
        public static native void printf( String format );
     
        public static void main( String[] args ) {
            printf( "Hello, GCJ World!n" );
        }
    }

    クラスファイル作成

    C:¥> gcj -C Hello.java

    C++ ヘッダファイル作成

    C:¥> gcjh Hello.class

    ソースコード(C++ ヘッダファイル)

     
    // DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
     
    #ifndef __Hello__
    #define __Hello__
     
    #pragma interface
     
    #include <java/lang/Object.h>
    #include <gcj/array.h>
     
    extern "Java"
    {
        class Hello;
    }
     
    class Hello : public ::java::lang::Object
    {
     
    public:
      Hello();
      static void printf(::java::lang::String *);
      static void main(JArray< ::java::lang::String * > *);
      static ::java::lang::Class class$;
    };
     
    #endif // __Hello__

    ソースコード(C++ 実装ファイル)

    #include <stdio.h>
    #include <java/lang/String.h>
    #include "Hello.h"
     
    void ::Hello::printf(::java::lang::String* format )
    {
        int len = format->length();
        for ( int i = 0; i < len; i++ )
        {
            ::printf( "%c", format->charAt(i) );
        }
    }

    コンパイル方法

    C:¥> gcj -c Hello.cpp
    C:¥> gcj --main=Hello -o Hello Hello.o Hello.class

    実行結果

    Hello, GCJ World!