3
votes

I need to call a method inside Jar from groovy script inside SoapUI project.

Due to the lack of administrative access I can't place that jar in "../bin/ext" folder in SaopUI intall directory.

So the only option left is load the jar in runtime and call the method. Very simple approach.

I tried the below approach.

this.class.classLoader.rootLoader.addURL(new URL("file:///H://Foo-2//Foo.jar"));
def cls = Class.forName("test").newInstance();
cls.add()

This is not working as rootLoader is null.

second approach .

def classLoader = ClassLoader.systemClassLoader
def newClassLoader = new URLClassLoader([new File("file:///H://Foo-2//Foo.jar")
        .toString().toURL()] as URL[], classLoader)
def cls = Class.forName("test").newInstance();

this is not working too , it's giving me ClassNotFoundException.

I spent a day in this. even change the class name to lower case after seeing this thread.

Edit 1

I tried this too. and change my code like this.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def classpathHacker = new com.eviware.soapui.support.ClasspathHacker ()
log.info "utils=" + groovyUtils
mystring = "file://H://Baz.jar" 
com.eviware.soapui.support.ClasspathHacker.addURL( new URL(mystring) )
def cls = new bar() // how to call the static method add of `bar` class ?
log.info cls

My Jar code is too simple. Here it is

public class bar {
    public static void main(String[] args) {
        add();
        }
        public static void add(){
            String path = "H:" + File.separator + "Groovy" + File.separator + "hi.txt";
            File f = new File(path);

            f.getParentFile().mkdirs(); 
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

What all the other option I have? What i am doing wrong?

Solved. Here is the final Groovy script.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def classpathHacker = new com.eviware.soapui.support.ClasspathHacker ()

path = groovyUtils.getProjectPath()
myfile = new java.io.File(path + "/Baz.jar")
mystring = "file://" + path + "/Baz.jar"
log.info "myfile=" + myfile

classpathHacker.addFile( myfile )
com.eviware.soapui.support.ClasspathHacker.addFile( myfile )
com.eviware.soapui.support.ClasspathHacker.addURL( new URL(mystring) )
//import Baz

def instance = this.class.classLoader.loadClass( 'bar', true, false )?.newInstance()
instance.add();
1
Isn't possible and simple to install in a directory where you the privileges? - Rao
I don't have the install right. yes it's hurts ! :( - John Doe

1 Answers

4
votes

Do you know about com.eviware.soapui.support.ClasspathHacker?
Maybe that is away to go about it, if you really cannot put it to the /ext folder.

Reference:

https://community.smartbear.com/t5/SoapUI-Open-Source/Soapui-is-not-loading-external-jar-file-location-added-to/td-p/7619

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def classpathHacker = new com.eviware.soapui.support.ClasspathHacker ()

log.info "utils=" + groovyUtils
path = groovyUtils.getProjectPath()
myfile = new java.io.File(path + "/ojdbc14.jar")
mystring = "file://" + path + "/ojdbc14.jar"
log.info "myfile=" + myfile

classpathHacker.addFile( myfile )
com.eviware.soapui.support.ClasspathHacker.addFile( myfile )
com.eviware.soapui.support.ClasspathHacker.addURL( new URL(mystring) )

import groovy.sql.*
def sql = groovy.sql.Sql.newInstance( db, userid, password, 'oracle.jdbc.driver.OracleDriver' )