如果您喜欢 java 并且拥有一个蓬勃发展的 java 生态系统,并且需要将 iris 纳入其中,那么这不是问题。 java 外部语言网关几乎可以无缝地完成它。该网关充当 iris 中 java 和对象脚本之间的桥梁。您可以在 iris 中创建 java 类的对象并调用它们的方法。您只需要一个 jar 文件即可完成此操作。

您需要做的第一件事是设置环境。 要开始使用 java 网关,请确保您具备以下条件:
第二个要求可能听起来很简单,因为您已经在工作中使用 java,但事实并非如此。由于这个问题,原来你最多需要使用jdk版本11。这意味着你需要更改 ide 中的版本,这可能会带来相当大的麻烦。
下一步,我们可以检查一切是否正常,并尝试实例化 java 系统类的对象。为此,您必须启动连接、创建代理对象并调用方法。听起来有很多代码,但实际上只有一条语句:
立即学习“Java免费学习笔记(深入)”;
write $system.external.getjavagateway().new("java.util.date").tostring()这将在屏幕上打印当前日期和时间。
为了更进一步,我们可以创建自己的 java 类:
public <span>class dish </span>{
private string name;
private string description;
private string category;
private float price;
private string currency;
private integer calories;
<span>public void setname(string name) </span>{
this.name = name;
}
<span>public void setdescription(string description) </span>{
this.description = description;
}
<span>public void setcategory(string category) </span>{
this.category = category;
}
<span>public void setprice(float price) </span>{
this.price = price;
}
<span>public void setcurrency(string currency) </span>{
this.currency = currency;
}
<span>public void setcalories(integer calories) </span>{
this.calories = calories;
}
<span>public string describe() </span>{
return "the dish "+this.name+" costs "+this.price.tostring()+
this.currency+" and contains "+this.calories+" calories!";
}
}
并从对象脚本调用它的方法:
set javaGate = $SYSTEM.external.getJavaGateway()
do javaGate.addToPath("D:\Temp\GatewayTest.jar")
set dish = javaGate.new("Dish")
do dish.setCalories(1000)
do dish.setCategory("salad")
do dish.setCurrency("GBP")
do dish.setDescription("Very tasty greek salad")
do dish.setName("Greek salad")
do dish.setPrice(15.2)
write dish.describe()结果我们将得到一个字符串,其中包含所创建菜肴的描述:
这道菜希腊沙拉售价15.2英镑,含有1000卡路里!
非常整洁,你不觉得吗?
无论如何,请从文档中了解有关使用网关的更多信息,祝您在项目中应用这些知识好运!
以上就是Java 外部语言网关的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号