Forum Discussion

santanu33's avatar
santanu33
New Contributor
7 years ago

How to load Jar inside Groovy Script dyanmically ? I don't have the access to "../bin/ext" folder

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.systemClassLoaderdef 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.

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

  • Access issues are frustrating some times, aren't they? Sometimes the day is better spent trying to get somebody to just put the jar in the appropriate location for you.

     

    If you can read your SoapUI install folder, but not write, can you make a copy of it somewhere else where you will have the ability to write the jars?

     

    Or, can you bring the Java content of the jar into a Groovy Script?

  • JHunt's avatar
    JHunt
    Community Hero

    Access issues are frustrating some times, aren't they? Sometimes the day is better spent trying to get somebody to just put the jar in the appropriate location for you.

     

    If you can read your SoapUI install folder, but not write, can you make a copy of it somewhere else where you will have the ability to write the jars?

     

    Or, can you bring the Java content of the jar into a Groovy Script?

  • santanu33's avatar
    santanu33
    New Contributor

    JHunt Thanks for the response, Yes they are frustrated. I am going to this thread. and change my code to 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()
    log.info cls

    Still not resolved. Any help.

     

    My super simple Jar source code

     

     

     

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