博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using JRuby with Maven
阅读量:6623 次
发布时间:2019-06-25

本文共 5543 字,大约阅读时间需要 18 分钟。

One of the features new to Java 6 is its built-in support for scripting languages (an introductory article on that topic can be found ), which enables you to load and execute programs written in a scripting language directly from within your Java program.

The scripting support in Java 6 is realized by providing an implementation of ("Scripting for the Java Platform"). While JavaScript is directly supported by the JDK itself, any other scripting language can be integrated as well by simply adding a JSR 223 compatible scripting engine to the class path.

The first place to look for JSR 223 scripting engines is , where engines for Ruby, Python, Groovy and a lot of other languages can be found.

To give it a try, I wanted to include the JRuby engine into a Maven based project. That engine can be found in the Maven repo at , but unfortunetaly its pom.xml is somewhat defective, as it contains the dependency script-api:javax.script, which neither exists in the java.net repository nor any other one I am aware of.

But when running on Java 6, it isn't required at all – as the scripting API is part of the JDK. So I excluded the dependency in the pom.xml of my own project, which reads as follows:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
4.0.0
org.gm
jruby-scripting
jar
1.0-SNAPSHOT
jruby-scripting
junit
junit
4.5
test
org.jruby
jruby
1.1.6
com.sun.script.jruby
jruby-engine
1.1.6
script-api
javax.script
maven2-repository.dev.java.net
Java.net Repository for Maven 2
http://download.java.net/maven/2/
maven-compiler-plugin
RELEASE
1.5
1.5

Having fixed that, we can try the scripting API by evaluating a simple Ruby script, that receives a variable provided by the hosting Java program and returns the obligatory String "Hello, jruby!" ;-):

12345678910111213141516171819202122232425262728
package org.gm.jrubyscripting;import static org.junit.Assert.*;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import org.junit.Test;public class HelloJRuby {
@Test public void helloJRuby() throws Exception {
String engineName = "jruby"; ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine jRubyEngine = manager.getEngineByName(engineName); assertNotNull(jRubyEngine); jRubyEngine.put("engine", engineName); assertEquals( "Hello, jruby!", jRubyEngine.eval("return 'Hello, ' + $engine + '!'")); }}

But what to do, if you are not running on Java 6? After some more searching I finally managed to find the missing dependency in the repo of the project, but with groupId and artifactId interchanged.

From there it can be added to the project, for example using a separate Maven to be activated on JDK versions < 1.6:

1234567891011121314151617181920212223
...
[1.3,1.6)
javax.script
script-api
1.0
dist.codehaus.org/mule
Mule Repository for Maven 2
http://dist.codehaus.org/mule/dependencies/maven2/
...

转载地址:http://wjtpo.baihongyu.com/

你可能感兴趣的文章
基于Token认证的WebSocket连接
查看>>
【Solidity】2.合约的结构体 - 深入理解Solidity
查看>>
《Drupal实战》——2.6 小结
查看>>
《C语言及程序设计》实践参考——二分法解方程
查看>>
java thread中的wait()和notify()
查看>>
2016最新搜索引擎优化(SEO)重点要素
查看>>
当Web访问性能出现问题,如何深探?
查看>>
【IOS-COCOS2D-X 游戏开发之二】【必看篇】总结阐述COCOS2D-X与COCOS2D-IPHONE区别;
查看>>
eoLinker-API_Shop_通讯服务类API调用的代码示例合集:短信服务、手机号归属地查询、电信基站查询等...
查看>>
前端面试回忆录 - 滴滴篇 - 凉面
查看>>
jxl导入Excel 切割List 并使用MyBatis批量插入数据库
查看>>
小程序开发总结
查看>>
Tomcat监听器设计思路
查看>>
管理ORACLE实例
查看>>
Confluence 6 MySQL 数据库设置准备
查看>>
Ruby 中 0/0.0 = NaN
查看>>
JEESNS数据库表设计结构
查看>>
JavaScript学习笔记:判断变量是否为undefined,判断变量和函数是否声明
查看>>
局域网访问Apache服务器
查看>>
JavaScript 闭包
查看>>