在 SoapUI 的 groovy 脚本中加载外部 jar

load external jar in groovy script in SoapUI

我需要从 SoapUI 项目中的 groovy 脚本调用 Jar 中的方法。

由于缺乏管理权限,我无法将该 jar 放在 SaopUI intall 目录的“../bin/ext”文件夹中。

所以剩下的唯一选择就是在运行时加载 jar 并调用方法。非常简单的方法。

我尝试了以下方法。

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

这不起作用,因为 rootLoader 是 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();

这也不起作用,它给了我 ClassNotFoundException.

我在这度过了一天。甚至在看到 thread.

后将 class 名称更改为小写

编辑 1

我试过这个 too。并像这样更改我的代码。

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

我的Jar代码太简单了。在这里

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();
            }
        }
}

我还有什么其他选择?我做错了什么?

已解决。这是最终的 Groovy 脚本。

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();

你知道com.eviware.soapui.support.ClasspathHacker吗?
如果你真的不能把它放到 /ext 文件夹中,也许就这样了。

参考:

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' )