How to add multiple jars to the classpath of groovyConole/groovysh?
Asked Answered
T

1

7

This feels ridiculous that I have to ask this, but I can't seem to add multiple jar files to the classpath for groovyConsole and groovysh. How do I add multiple jar files to the classpath? Here is what I've tried:

groovyConsole -cp ~/lib/compile/jar/mysql-connector-java-5.1.32.jar:~/lib/compile/jar/ojdbc6-11.2.0.3.jar

However, when I execute this code in the console I get the following:

 file:/Users/charlie/lib/compile/jar/mysql-connector-java-5.1.32.jar
 file:/Users/charlie/projects/fuseanalytics/./
 file:/Users/charlie/.gvm/groovy/current/lib/ant-1.9.3.jar
 file:/Users/charlie/.gvm/groovy/current/lib/ant-antlr-1.9.3.jar
 file:/Users/charlie/.gvm/groovy/current/lib/ant-junit-1.9.3.jar
 file:/Users/charlie/.gvm/groovy/current/lib/ant-launcher-1.9.3.jar
 file:/Users/charlie/.gvm/groovy/current/lib/bsf-2.4.0.jar
 file:/Users/charlie/.gvm/groovy/current/lib/commons-cli-1.2.jar
 file:/Users/charlie/.gvm/groovy/current/lib/commons-logging-1.1.1.jar
 file:/Users/charlie/.gvm/groovy/current/lib/gpars-1.2.1.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-ant-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-bsf-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-console-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-docgenerator-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-groovydoc-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-groovysh-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-jmx-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-json-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-jsr223-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-nio-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-servlet-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-sql-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-swing-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-templates-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-test-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-testng-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/groovy-xml-2.3.6.jar
 file:/Users/charlie/.gvm/groovy/current/lib/hamcrest-core-1.3.jar
 file:/Users/charlie/.gvm/groovy/current/lib/ivy-2.3.0.jar
 file:/Users/charlie/.gvm/groovy/current/lib/jansi-1.11.jar
 file:/Users/charlie/.gvm/groovy/current/lib/jcommander-1.35.jar
 file:/Users/charlie/.gvm/groovy/current/lib/jline-2.11.jar
 file:/Users/charlie/.gvm/groovy/current/lib/jsp-api-2.0.jar
 file:/Users/charlie/.gvm/groovy/current/lib/jsr166y-1.7.0.jar
 file:/Users/charlie/.gvm/groovy/current/lib/junit-4.11.jar
 file:/Users/charlie/.gvm/groovy/current/lib/multiverse-core-0.7.0.jar
 file:/Users/charlie/.gvm/groovy/current/lib/qdox-1.12.1.jar
 file:/Users/charlie/.gvm/groovy/current/lib/servlet-api-2.4.jar
 file:/Users/charlie/.gvm/groovy/current/lib/testng-6.8.8.jar
 file:/Users/charlie/.gvm/groovy/current/lib/xmlpull-1.1.3.1.jar
 file:/Users/charlie/.gvm/groovy/current/lib/xstream-1.4.7.jar

Notice that the first jar file, the mysql jar file, is added to the classloader, but the second jar file, the oracle jar file, is not added. I've also tried using the -cp argument twice like:

 groovyConsole -cp ~/lib/compile/jar/mysql-connector-java-5.1.32.jar -cp ~/lib/compile/jar/ojdbc6-11.2.0.3.jar

But that just fails with an error. The docs are conspicuously devoid of any guidance other than vague hints that it works just like Java. Hence why its so frustrating the first attempt didn't work out of the box.

I've also tried to add the Jar files after starting groovyConsole using Script > Add Jar(s) to Class path menu option, but it adds none of the jar files.

So why doesn't this work in a more straight forward manner?

Tidings answered 6/10, 2014 at 2:15 Comment(4)
Hmm it appears that the groovyConsole has a problem with the ~/ in it. For some reason the shell isn't expanding it for the second one. Be nice if groovyConsole said something if it couldn't find the jar file.Tidings
Use $HOME then. ~ is not handled by the shell, but must be handled by the app/java.Reannareap
Setting the CLASSPATH environment variable has been the only thing that worked for me. PuzzledDecaliter
I had problems with menu "add jar to classpath", Broader problem is here: #32495335Coopery
T
6

The Groovy classpath is the Java classpath, and you can include all the JARs in a directory using a wildcard (note the this is not the Unix wildcard, but a feature of Java, and you may have to enclose it in single quotes on Unix to prevent it from being expanded. To wit:

groovyConsole -cp $HOME/lib/compile/jar/'*'

This took me a disturbingly long time to locate: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html#A1100762

Trudeau answered 9/1, 2019 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.