JavaScript可以使用jar文件中的类(非Internet使用) [英] Can JavaScript use classes from a jar file (Non Internet Use)

查看:181
本文介绍了JavaScript可以使用jar文件中的类(非Internet使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jar文件中有一个API但是如何在JavaScript中使用jar中的类?当我尝试导入它们时,

I have an API in a jar file but how can I use the classes from the jar in JavaScript? When I try importing them,

    conf = Packages.abcapi.Config;
    var cfg = new conf.Config();

它不起作用。这不会在浏览器或互联网上使用。

It doesn't work. This is not going to be used in a browser or over the internet.

更新:

我是使用Java ScriptEngine将我们的API扩展到所有JSR-223脚本语言。在Java应用程序内部,我读取了一个JavaScript文件,然后使用ScriptEngine执行该文件。我需要JavaScript文件来使用API​​中的类,它们位于一个jar文件中。我在运行ScriptEngine时尝试在类路径中设置jar,但它仍然没有使用上面的代码找到类。这在Jython中运行良好,因为在类路径中设置jar后,在Jython中使用jar文件中的类没有问题。

I'm extending our API to all JSR-223 Scripting Languages using Java ScriptEngine. Inside the Java application I read a JavaScript File and then execute the file using ScriptEngine. I need for the JavaScript File to use classes from the API which lays in a jar file. I try setting the jar in the classpath when running the ScriptEngine but it still doesn't find the classes using the above code. This works fine in Jython though, as in Jython has no problem using the classes in the jar file after setting the jar in the class path.

推荐答案

我正在阅读JavaScript文件,然后使用Java ScriptEngine执行它们。当您运行要执行JavaScript的Java代码时,将jar添加到类路径中,

I am reading JavaScript files then executing them using Java ScriptEngine. Add the jar to your class path when you run the Java code that's going to execute the JavaScript like this,

    java -cp ./;C:\Location\Of\The\Jar\File\abc.jar MainClass

然后在JavaScript中,您可以从该jar中获取包并将包设置为变量。然后,当您想要使用该包中的Class时,只需在类名前加上变量,然后使用a。像这样,

Then inside JavaScript you can get the package from within that jar and set the package to a variable. Then when you want to use a Class from that package just prefix the class name with the variable and then a . like this,

    myvariable = Packages.abc.foo.pack.name;

    var foo = new myvariable.ClassFromTheJarFile("arg1","arg2");

    foo.doSomething();

    var fooSister = new myvariable.AnotherCLassFromTheJarFile("arg1");

    fooSister.doSomthingFromThisClass();

我做错了就是尝试直接从包中导入类,

What I did wrong was try to import the class directly from the package like this,

    myvariable = Packages.abc.foo.pack.name.ClassFromTheJarFile;

所以只导入包而不是类。为了澄清你不需要使用Java *

So only import the package and not the classes. To clarify you do not need to use the Java *.

这不是在浏览器中使用或以上互联网。这是另一个使用来自URL位置的jar的链接,它可能对某人有帮助 http://mozilla-firefox-extension-dev.blogspot.com/2004/11/calling-java-code-in-custom-jars-from。 html

This is not being using in a browser or over the internet. Here is another link that uses a jar from a URL location and it might be helpful to someone http://mozilla-firefox-extension-dev.blogspot.com/2004/11/calling-java-code-in-custom-jars-from.html

这篇关于JavaScript可以使用jar文件中的类(非Internet使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆