Dušan Rychnovský
Dušan Rychnovský

Reputation: 12459

Groovy - class not found

The following Groovy script fails with a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception.

@Grapes([
    @Grab('mysql:mysql-connector-java:5.1.25')
])

import groovy.sql.Sql

def sql = Sql.newInstance(
    'jdbc:mysql://localhost/books', 
    'root',
    '', 
    'com.mysql.jdbc.Driver'
);

I looked into the JAR file stored at C:\Users\Dusan\.groovy\grapes\mysql\mysql-connector-java\jars\mysql-connector-java-5.1.25.jar and it contains the Driver class.

What can be wrong?

Upvotes: 14

Views: 6657

Answers (2)

Francois Lacoursiere
Francois Lacoursiere

Reputation: 131

How do you use it in groovysh ?

As per the doc, Grab is used in the shell this way

groovy.grape.Grape.grab([group:'mysql:mysql-connector-java:5.1.25'])

I haven't found the equivalent for @GrabConfig. It simply does not work inside groovysh.

Upvotes: 1

tim_yates
tim_yates

Reputation: 171114

You need:

@GrabConfig(systemClassLoader = true)

After your @Grab, and just:

@Grab('mysql:mysql-connector-java:5.1.25')
@GrabConfig(systemClassLoader = true)
import groovy.sql.Sql

def sql = Sql.newInstance(
    'jdbc:mysql://localhost/books', 
    'root',
    '', 
    'com.mysql.jdbc.Driver'
)

Should do

Upvotes: 25

Related Questions