2

I have jar file with following code:

MyClass.jar

package com.sample;
public class MyClass {
    public static String getName() {
        return "Hii";
    }
}

I want to load MyClass.jar in javascript and call getName method.
I've tried following code

var cl = new Packages.java.net.URLClassLoader(
        [new Packages.java.net.URL('C:\\tmp\\MyClass.jar')]);
var aClass = Packages.java.lang.Class.forName("MyClass", true, cl);
var aStaticMethod = aClass.getMethod("getName", []);
var greeting = aStaticMethod.invoke(null, []);  

When I ran the above code I'm getting error "Packages is not defined".

Actually I'm running the above Javascript code in Loadrunner Web Http/html protocol script.

I kept the jar file in Loadrunner bin folder and tried below code.

function myfun() {
    var myvariable = Packages.com.sample;    
    var foo = new myvariable.MyClass();    
    return foo.getName();
}

I'm getting same error "Packages is not defined"

Kindly suggest.

4
  • This is not the correct duplicate referenced... What happens if you remove the "Packages." in your code? Commented Apr 8, 2019 at 8:45
  • @SirFartALot if I remove Packages..it is saying java is not define.. Commented Apr 8, 2019 at 9:30
  • @Ghostcat I cheked the proposed solution...I need to implement the solution in loadrunner...could not understand how to put the jar in the loadrunner classpath Commented Apr 8, 2019 at 9:38
  • Then please add some more details there, including that you researched that DUP question, and explain how it doesn't work for you. Commented Apr 8, 2019 at 9:39

1 Answer 1

0

lr.loadLibrary() function will be used to import external js to the script but not sure will it import jar files as well. Just give it a try, If not try with JAVA over HTTP protocol- This will definitely help with your requirements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.