简体   繁体   English

如何在 Java Script 中导入 .Jar 文件

[英]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 !我想在 Java Script 中导入一个 .Jar 文件,Jar 文件中有一个类,我想在 Java 脚本中使用它,这是我找到但无法使其工作的代码!

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. JavaScript 不是 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.并且试图在你的 JavaScript ExtJS 代码中使用 Java 代码将是一个完全的矫枉过正,要求用户在他们的浏览器中安装 Java 插件,并且因为 Java 插件而减慢他们的浏览器。 Of course, supposing it might work (and I'm not sure about that).当然,假设它可能有效(我不确定)。

You want to call webservices from your JavaScript code?您想从您的 JavaScript 代码调用网络服务吗? (Re)Write the webservice-calling in JavaScript. (重新)用 JavaScript 编写 webservice-calling。 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.这不仅是最好的解决方案,而且我很确定它比尝试设置 Java 小程序并尝试与之交互所需的工作更少。

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.如果您确实尝试编写带有 Java 后端的 JavaScript 应用程序,您可能很难从 JavaScript 社区获得所需的所有支持。 But if you think of it as a Java application with a JavaScript frontend, all of a sudden, you have lots of options.但是,如果您将其视为具有 JavaScript 前端的 Java 应用程序,那么突然之间,您就有了很多选择。 I'm sure SpringBoot alone could give you many complete solutions.我相信仅 SpringBoot 就可以为您提供许多完整的解决方案。 Here's one for React.js .这是React.js的一个。

Furthermore, feel free to look at some of these new frameworks I found at Raygun.com .此外,请随意查看我在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 .它被列为Vaadin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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