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.

M Hamza Javed
  • 1,269
  • 4
  • 17
  • 31
Svp57
  • 308
  • 2
  • 13
  • This is not the correct duplicate referenced... What happens if you remove the "Packages." in your code? – SirFartALot Apr 08 '19 at 08:45
  • @SirFartALot if I remove Packages..it is saying java is not define.. – Svp57 Apr 08 '19 at 09: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 – Svp57 Apr 08 '19 at 09:38
  • Then please add some more details there, including that you researched that DUP question, and explain how it doesn't work for you. – GhostCat Apr 08 '19 at 09:39

1 Answers1

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.

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
rollno748
  • 334
  • 2
  • 7