【问题标题】:Import java library to a x10 class将 java 库导入 x10 类
【发布时间】:2015-07-17 07:35:15
【问题描述】:

我正在用 x10 语言做一个项目。它的后端是java。我需要将一些有用的 java 库导入 x10 类。但是当我用 ant 构建项目时,它会给出构建错误,说找不到导入的类名。然后我尝试将它们导入java类。我成功了。

但我需要的是,将它们导入 x10 类。

【问题讨论】:

  • 您的意思是添加 x10 语言标签而不是 x10?

标签: java ant import libraries x10-language


【解决方案1】:

你需要做两件事。

  1. 在x10程序中,添加import声明(如import java.util.HashMap)
  2. 在调用 x10c 编译器时,确保要使用的 Java 类的 .class 文件位于源路径中。如果您要导入属于 Java 标准库 (java.*) 的类,这应该可以正常工作。如果您要导入自己的类,则需要使用 -cp 命令行参数告诉 x10c 这些类。

samples/java.interop 目录中的一些 X10/Java 互操作示例可能会有所帮助。以下是其中之一供参考:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


/**
 * An example to show how to use Apache HttpComponents HttpClient 4.2.1 in X10.
 * Compile as "x10c -cp httpclient-4.2.1.jar:httpcore-4.2.1.jar HttpClient.x10"
 * Run as as "x10 -cp .:httpclient-4.2.1.jar:httpcore-4.2.1.jar:commons-logging-1.1.1.jar HttpClient"
 */
public class HttpClient {
    static val url = "http://targethost/";

    public static def main(Rail[String]):void {
        finish for (p in Place.places()) {
            at (p) async {
                val ncores = java.lang.Runtime.getRuntime().availableProcessors();
                Console.OUT.println("Place " + p.id + " has " + ncores + " cores.");
                finish for (var i:Int = 0n; i < ncores; ++i) {
                    val coreid = i;
                    async {
                        Console.OUT.println("Place " + p.id + " core " + coreid + " start.");
                        val httpclient = new DefaultHttpClient();
                        val httpGet = new HttpGet(url);
                        while (true)
                        {
                            val response1 = httpclient.execute(httpGet);
                            /*
                             * The underlying HTTP connection is still held by the response object 
                             * to allow the response content to be streamed directly from the network socket. 
                             * In order to ensure correct deallocation of system resources 
                             * the user MUST either fully consume the response content  or abort request 
                             * execution by calling HttpGet#releaseConnection().
                             */
                            try {
                                Console.OUT.println(response1.getStatusLine());
                                val entity1 = response1.getEntity();
                                /* do something useful with the response body and ensure it is fully consumed */
                                EntityUtils.consume(entity1);
                            } finally {
                                httpGet.releaseConnection();
                            }
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    x10 2.6.1 以这种方式与 Java 8 开箱即用。

    (Java 9 / 10 有一个可以改变事物的模块系统。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2011-11-24
      • 1970-01-01
      • 2012-07-01
      • 2019-01-25
      相关资源
      最近更新 更多