简体   繁体   中英

Call java class defined in jar from Javascript

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM