如何在 Java 脚本中导入 .Jar 文件

发布于 2024-09-29 07:36:34 字数 606 浏览 0 评论 0原文

我想在 Java 脚本中导入 .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);

任何建议将不胜感激

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天生の放荡 2024-10-06 07:36:34

你不知道。

JavaScript 不是 Java。

尝试在 JavaScript ExtJS 代码中使用 Java 代码将完全是一种矫枉过正,要求用户在浏览器中安装 Java 插件,并且由于 Java 插件而降低浏览器的速度。当然,假设它可能有效(我对此不确定)。

您想从 JavaScript 代码调用 Web 服务吗? (重新)用 JavaScript 编写 Web 服务调用。 这不仅是最好的解决方案,而且我非常确定它比尝试设置 Java 小程序并尝试与其交互所需的工作量更少。

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.

飘逸的'云 2024-10-06 07:36:34

还有另一种思考方式。如果您确实尝试使用 Java 后端编写 JavaScript 应用程序,那么您可能很难从 JavaScript 社区获得所需的所有支持。但如果您将其视为具有 JavaScript 前端的 Java 应用程序,那么突然间您就有了很多选择。我确信仅 SpringBoot 就可以为您提供许多完整的解决方案。这是 React.js 的一个。

此外,请随意查看我在 Raygun.com。我自己还没有机会使用其中任何一个,但我相信其中一个似乎特别符合您正在寻找的东西。它被列为 Vaadin

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文