How do I auto load a database jar in Groovy without using the -cp switch?

I want to simplify my execution of a Groovy script that makes calls to an Oracle database. How do I add the ojdbc jar to the default classpath so that I can run:

groovy RunScript.groovy

instead of:

groovy -cp ojdbc5.jar RunScript.groovy


Asked by: Michael544 | Posted: 21-01-2022






Answer 1

Summarized from Groovy Recipes, by Scott Davis, Automatically Including JARs in the ./groovy/lib Directory:

  1. Create .groovy/lib in your login directory
  2. Uncomment the following line in ${GROOVY_HOME}/conf/groovy-starter.conf

    load !{user.home}/.groovy/lib/*.jar

  3. Copy the jars you want included to .groovy/lib

It appears that for Groovy 1.5 or later you get this by default (no need to edit the conf), just drop the jars in the /lib dir.

Answered by: Emily377 | Posted: 22-02-2022



Answer 2

There are a few ways to do it. You can add the jar to your system's CLASSPATH variable. You can create a directory called .groovy/lib in your home directory and put the jar in there. It will be automatically added to your classpath at runtime. Or, you can do it in code:

this.class.classLoader.rootLoader.addURL(new URL("file:///path to file"))

Answered by: Anna751 | Posted: 22-02-2022



Answer 3

One way would be using @Grab in the code:

    @GrabConfig(systemClassLoader=true)
    @Grab('com.oracle:ojdbc6:12.1.0.2.0')
    Class.forName("oracle.jdbc.OracleDriver").newInstance()

Answered by: Walter611 | Posted: 22-02-2022



Answer 4

groovy is just a wrapper script for the Groovy JAR that sets up the Java classpath. You could modify that script to add the path to your own JAR, as well, I suppose.

Answered by: Cherry827 | Posted: 22-02-2022



Answer 5

You could add the following shebang to the first line of your Groovy script:

#!/usr/bin/env groovy -cp ojdbc5.jar

Then, mark the script executable:

chmod u+x RunScript.groovy

Now, running the script by itself will set the classpath automatically.

./RunScript.groovy

Answered by: Julia673 | Posted: 22-02-2022



Similar questions

Java version names in Sun's bug database

In http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6525150 it says Release Fixed 7(b14), 6u2(b01) (Bug ID:2147561) What does 6u2 mean? (Am I right in assuming it as 1.6.0_02 ?)


java - Ways to save enums in database

What is the best way to save enums into a database? I know Java provides name() and valueOf() methods to convert enum values into a String and back. But are there any other (flexible) options to store these values? Is there a smart way to make enums into unique numbers (ordinal() is not safe to use)? Update Thanks for all awesome and fast answers! It was ...


Java Database Poller?

Are there any JAVA API's for polling a database. Should be able to a) Be able to get all the data from a table to start with. b) Poll it every configurable minutes.


java - Connect to database using jsf

How do I connect to the database(MYSQL) in connection bean using JSF to retrieve its contents. Also please let me know how do I configure the web.xml file?


java - Connect to a Read Only database

While trying to connect to a database that's set to read only, the connection cannot be made. I get the following error: Cannot create PoolableConnectionFactory (Io exception: The Network Adapter could not establish the connection) The application needs to run in read-write mode and then automatically handle the switch to read-only by only checking the isReadOnly property of the connecti...


Adding Java Objects to database

For a university assignment I have been assigned I have a Prize object which contains either text, image, or video content. I would like to persist this information into a BLOB field within an Apache Derby database (which will be running on a low powered PDA). How can I add this data to the database? Thanks in advance.


java - Apache Derby - Check Database Already Created?

Using Apache Derby with Java (J2ME, but I don't think that makes a difference) is there any way of checking if a database already exists and contains a table?


database - Handling abnormal Java program exits

Suppose I have a Java application that opens a database connection. Normally I would add a connection.close() in a finally block, but this block wouldn't be executed in the case of a kill operation, or any other abnormal termination, would it? Are there any other precautions that I, as a programmer, can make in order to close the connection properly before the application exits?


mysql - Adding Image to a database in Java

I am trying to add an image to a BLOB field in a mysql database. The image is going to be less then 100kb in size. However I am running into problems and was wondering what would be a better way to add this data to the database? com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'Data' at row 1 PreparedStatement addImage = conn.prepareStatement("INSERT INTO Images (Width, ...


java - Using an enum as a mapkey results in a RAW in the database

I'm trying to use an enum as a mapkey for a map in Hibernate, but Hibernate stores my enum as a RAW: I have this enum: public enum AccountType implements Serializable { CASH, CREDIT_CARD, GIRO, INVOICE, OTHER; } Which I'm trying to use as a key in a map: @CollectionOfElements @MapKey(columns = @Column(name="ACC_TYPE"), targetElement = Account...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top