Java 外部语言网关
收藏
欢迎各位小伙伴来到,相聚于此都是缘哈哈哈!今天我给大家带来《Java 外部语言网关》,这篇文章主要讲到等等知识,如果你对文章相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
如果您喜欢 java 并且拥有一个蓬勃发展的 java 生态系统,并且需要将 iris 纳入其中,那么这不是问题。 java 外部语言网关几乎可以无缝地完成它。该网关充当 iris 中 java 和对象脚本之间的桥梁。您可以在 iris 中创建 java 类的对象并调用它们的方法。您只需要一个 jar 文件即可完成此操作。
您需要做的第一件事是设置环境。 要开始使用 java 网关,请确保您具备以下条件:
- intersystems iris:已安装并正在运行。
- java 开发工具包 (jdk): 已安装并配置。
第二个要求可能听起来很简单,因为您已经在工作中使用 java,但事实并非如此。由于这个问题,原来你最多需要使用jdk版本11。这意味着你需要更改 ide 中的版本,这可能会带来相当大的麻烦。
下一步,我们可以检查一切是否正常,并尝试实例化 java 系统类的对象。为此,您必须启动连接、创建代理对象并调用方法。听起来有很多代码,但实际上只有一条语句:
write $system.external.getjavagateway().new("java.util.date").tostring()
这将在屏幕上打印当前日期和时间。
为了更进一步,我们可以创建自己的 java 类:
public class dish { private string name; private string description; private string category; private float price; private string currency; private integer calories; public void setname(string name) { this.name = name; } public void setdescription(string description) { this.description = description; } public void setcategory(string category) { this.category = category; } public void setprice(float price) { this.price = price; } public void setcurrency(string currency) { this.currency = currency; } public void setcalories(integer calories) { this.calories = calories; } public string describe() { 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 外部语言网关》文章吧,也可关注公众号了解相关技术文章。
版本声明 本文转载于:dev.to 如有侵犯,请联系删除