简体   繁体   中英

how to import a .Jar file in Java Script

I want to import a .Jar file in Java Script, there is a Class in the Jar file which i want to use in Java script,and here is a code i found but couldn't get it work yet !

var cl = new Packages.java.net.URLClassLoader(
               [
                   new Packages.java.net.URL('http','My Ip', '8080', 'Hello.jar')
               ]
        );

        var aClass = Packages.java.lang.Class.forName("PackageAddress.Hello", true, cl);// Hello is a public class name
        var aStaticMethod = aClass.getMethod("SayHello", []);//SayHello is a public method returning a string
        var greeting = aStaticMethod.invoke(null, []);
        alert(greeting);

Any Suggestions would be appreciated

You don't.

JavaScript is not Java.

And trying to use Java code within your JavaScript ExtJS code will a complete overkill, requiring users to have Java plugin installed in their browsers, and slowing down their browsers because of Java plugin. Of course, supposing it might work (and I'm not sure about that).

You want to call webservices from your JavaScript code? (Re)Write the webservice-calling in JavaScript. Not only this is the best solution, but I'm pretty sure it will require less work than trying to set up a Java applet and trying to interact with it.

There's another way to think of this. If you are trying to indeed write a JavaScript application with a Java backend, you probably would struggle to get all the support you need from the JavaScript community. But if you think of it as a Java application with a JavaScript frontend, all of a sudden, you have lots of options. I'm sure SpringBoot alone could give you many complete solutions. Here's one for React.js .

Furthermore, feel free to look at some of these new frameworks I found at Raygun.com . I haven't had the opportunity to use any of these myself but I believe one in particular seems to have the make up of what you're looking for. It was listed as Vaadin .

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