Connecting to postgresql from a groovy script executed at command line - groovy

I'm trying to run a groovy script which loads data into a Postgresql database. It runs perfectly using IntelliJ but fails when run from the windows command line with the following error:
Caught: java.lang.ClassNotFoundException: org.postgresql.Driver
java.lang.ClassNotFoundException: org.postgresql.Driver
at UploadStressTestData.run(UploadStressTestData.groovy:118)
Apparently, the groovy script can't find the driver class. I need to somehow make that available or loadable from that script.
My question is, how do I do that? Do I need to use the -cp option and tell it where the postgresql driver is?
Any help vastly appreciated.

groovy -cp C:\Users\someuser\workspace\lib\postgresql-42.2.6.jre7.jar UploadStressTestData.groovy

In the simplest case you should be using #Grab to pull dependencies for your scripts:
#Grab('org.postgresql:postgresql:42.2.6')
// do your scripting stuff

Related

Unable to open Pyspark Shell

I am running ./bin/pyspark at to open the pyspark shell but I get the following error :
`py4j.protocol.PY4JNetworkError: An error occurred while trying to connect to the Java Server`
Searched about it a lot. Couldn't find a solution.
How should I resolve this ?
Thanks,
This issue seems like your Java issue
Please use commands to check your java process in your machine
ps -ef|grep java
If doesn't works then please try to update your Java JDK to new.
Download:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

GroovyConsole Adding dependent jars

I am running GroovyConsole on a mac (to launch I just do groovyConsole from the command line). I am a newbie so not sure how I can import needed libraries (jars). For example suppose my groovy script needs apache commons httpclient.
Do I need to specify the jar names in the command line when I open groovyConsole or should I be using Grapes? If it is the latter, I am not sure, the exact syntax of the Grab command. I tried the below and it didn't work...
#Grapes([
#Grab('org.apache:commons-httpclient:3.1'),
#GrabConfig(systemClassLoader = true)])
import org.apache.commons.httpclient.Credentials
Any help would be much appreciated.
You can either do just
#Grab('org.apache:commons-httpclient:3.1')
Or you can add jars from the menu in the groovy console itself

Not able to execute groovy script from command ".GroovyRuntimeException: Conflicting module versions."

I have run groovy script for a while from command line. Recently I have started to work with spock/groovy in the eclipse. I have run couple of test using Groovy Console when console was loaded from eclipse. Any way after these activities I have tried to execute groovy from command line and it failed with the following error:
Caused by: groovy.lang.GroovyRuntimeException: Conflicting module versions. Module [groovy-xml is loaded in version 2.4.3 and you are trying to load version 2.4.1
I was using gvm tool to reinstall groovy and set is a primary version but still have the problem. Something was changed from eclipse where my groovy installation gets affected and can not be fixed by by reinstalling.
I have some search done but did not find solution. most solution were mentioning this problem but they were trying to fix this withing eclipse, where in my case I am trying to fix it outside of eclipse. Also will be nice if I be able to prevent this from happening in the future. If somebody have similar experience please share.
Thanks,
Alexander

Running a groovy script in neo4j shell using gsh gives "java.lang.ClassNotFoundException: groovy.lang.Binding"

Running a groovy script in neo4j shell using gsh gives "java.lang.ClassNotFoundException: groovy.lang.Binding".
Even a simple script command like gsh --test
with the following file in the GSP_PATH fails:
test.groovy:
println "test!"
What are the steps to solve this?
The groovy dependency is missing:
Just copy the groovy jar into your lib directory
cp /Users/mh/Downloads/groovy-2.3.7/embeddable/groovy-all-2.3.7.jar path/to/neo4j-community-2.1.5/lib/

run Groovy CLI from groovy jar placed in war

I know about Groovysh, but I need to know if it is possible to run groovy CLI directly from groovy.jar placed in deployed war servlet (using one command). I can simplify question. Is there possibility to run Groovy CLI like it works, for example, in Clojure?
java -cp clojure-1.4.0.jar clojure.main
And CLI appears in terminal. This is how things look in clojure. I am looking for one line command which will run groovy CLI in terminal (using only groovy library to run it). I was looking for help in javadoc, found classes which should help, but don't know how to run it. :f
http://groovy.codehaus.org/api/groovy/lang/GroovyShell.html
(If someone knows solution which doesn't meet all criterias, also please answer.)
//EDIT
It seems that it needs groovy.jar, commons-cli.jar, antlr.jar, asm-util.jar and jline.jar. So I've added those files in my war file in WEB-INF/lib directory. Maybe it's good solution to make my own jar which role will be to call Groovy CLI from other jars, but now the question is, how ro run jar placed in WEB-INF/lib directory inside deployed war application via command line?
Greets
Seems like groovysh depends on more libs than the embeddable version contains. I managed to do what you want by using the following command:
$ java -classpath '*' org.codehaus.groovy.tools.shell.Main
But the current directory had to be the dir where all the groovy libs are; i.e. $GROOVY_HOME/lib:
$ cd $GROOVY_HOME/lib
$ java -classpath '*' org.codehaus.groovy.tools.shell.Main
Groovy Shell (2.0.6, JVM: 1.6.0_24)
Type 'help' or '\h' for help.
------------------------------------------------------------------------------
groovy:000>

Resources