(Java 9/10有一个模块系统来改变事物)
我正在做一个x10语言的项目。它的后台是java。我需要导入一些有用的java库到x10类。但是,当我用ant构建项目时,它会给出构建错误,说没有找到导入的类名。然后我尝试将它们导入到java类中。我成功了。
但我需要的是,将它们导入到x10类
您需要做两件事。
- 在x10程序中添加导入声明(例如import java.util.HashMap)
- 确保在调用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();
}
}
}
}
}
}
}
}
x10 2.6.1以这种方式在Java 8中开箱即用。