加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Running standalone Groovy scripts

发布时间:2020-12-14 16:52:57 所属栏目:大数据 来源:网络整理
导读:总结: 用java运行groovy的两种方式: 1)只运行groovy脚本: java -jar groovy-all.jar hello.groovy 2) groovy用到了java的jar包 java -cp groovy-all.jar;mylib.jar?groovy.ui.GroovyMain hello.groovy http://www.vasanth.in/2011/06/29/running-standal

总结:

用java运行groovy的两种方式:

1)只运行groovy脚本:

java -jar groovy-all.jar hello.groovy

2) groovy用到了java的jar包

java -cp groovy-all.jar;mylib.jar?groovy.ui.GroovyMain hello.groovy



http://www.vasanth.in/2011/06/29/running-standalone-groovy-scripts/


Running standalone Groovy scripts

As I mentioned in my post?Learning Groovy,I am writing Groovy scripts to automate some simple tasks. Now I want to share the scripts with others. I did not want them to install Groovy just to run the scripts. I had the groovy-all-1.8.0.jar,so I typed the following in the command prompt:

1
2
C:temp >java -jar groovy-all-1.8.0.jar Hello.groovy
Hello

It worked. Simple. Now I tried to run my script that retrieved the XMLs from a database.

1
2
3
4
C:workFetchData >java -jar groovy-all-1.8.0.jar FetchData.groovy
Caught: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at FetchData.fetch (FetchData.groovy:51 )
at FetchData.run (FetchData.groovy:3 )

Of course I need the driver jar in the classpath. So I added the sqljdbc.jar to the classpath.

1
2
3
4
C:workFetchData >java -cp sqljdbc.jar -jar groovy-all-1.8.0.jar FetchData.groovy
Caught: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
? ? ? ? at FetchData.createCSV (FetchData.groovy:51 )
? ? ? ? at FetchData.run (FetchData.groovy:3 )

Looks like the script is run with a different classloader and the java classpath is not passed to it. I did some?lazy googling?but did not find the answer I was looking for. So I looked at how eclipse is doing it. Eclipse used GroovyStarter and GroovyMain to run scripts. Not knowing which to use,I just used GroovyMain:

1
2
C:workFetchData >java -cp lib/groovy-all-1.8.0.jar;lib/sqljdbc.jar groovy.ui.GroovyMain FetchData.groovy
Fetched Data !

It worked! I just added the command in a batch file to share the script.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读