Import java library to a x10 class

229 views Asked by At

I am doing a project in x10 language. Its back-end is java. I need to import some useful java libraries to x10 class. But when I build the project with ant, it gives build errors saying imported class name is not found. Then I tried to import them to a java class. I succeeded.

But what I need is, to import them to a x10 class.

2

There are 2 answers

0
Dave Grove On BEST ANSWER

There are two things you need to do.

  1. In the x10 program, add import declarations (eg import java.util.HashMap)
  2. Make sure the .class files for the Java classes you want to use are in the sourcepath when you invoke the x10c compiler. If you are importing classes that are part of the Java standard library (java.*), this should just work. If you are importing your own classes, you will need to tell x10c about the classes by using the -cp command line argument.

There are some examples of X10/Java interop in the samples/java.interop directory that may help. Here is one of them for reference:

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();
                            }
                        }
                    }
                }
            }
        }
    }
}
0
Vijay Saraswat On

x10 2.6.1 works out of the box in this way with Java 8.

(Java 9 / 10 have a module system that changes things.)