0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

x10でJavaのメソッドを呼ぶ

Last updated at Posted at 2015-03-27

Managed版(Java backend))のx10から、Javaで実装されたクラスを使う方法です。
並列化の部分をx10にまかせつつ、既存のJavaの資産を使うことができます。
Java interoperability feature とよばれています。

参考資料:

まずJavaのクラスを用意します。

MyClass.java
class MyClass {

  int i;

  MyClass() {
    System.out.println("Constructed.");
    i = 5;
  }

  public int GetI() {
    return i;
  }
}

Javaのコードをビルドします。 javac MyClass.java

次にx10側のコードです。

MyNativeClass.x10
import x10.io.Console;
import x10.interop.Java;               // x10.interop.Java をimportする

public class MyNativeClass {

  public static def main( args:Rail[String] ) {
    val o = new MyClass();             // Javaのオブジェクトをnewする
    Console.OUT.println( o.GetI() );   // インスタンスメソッドを呼ぶ
  }
}

重要な点は import x10.interop.Java を書くことです。すると普通にJavaのオブジェクトが呼べます。
コンパイルするときは、クラスファイルがあるパスを -cpオプションで指定します。
事前にjavacでjavaのコードをコンパイルしておく必要があります。

コンパイルと実行のコマンドは以下のようになります。

x10c -cp . MyNativeClass.x10     # カレントディレクトリでも -cp で指定する必要あり
x10 MyNativeClass                # カレントディレクトリであれば-cpはいらない
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?