通过Java ScriptEngine在JavaScript中使用jar [英] Use a jar in JavaScript through Java ScriptEngine

查看:488
本文介绍了通过Java ScriptEngine在JavaScript中使用jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JavaScript中的jar文件中的类。我正在通过Java ScriptEngine使用JavaScript,并希望做类似于我在这里使用Jython所做的事情,

I need to use classes from a jar file inside JavaScript. I'm using JavaScript through Java ScriptEngine and would like to do something similar to what I did with Jython here,

    import org.python.core.Py;
    import org.python.core.PySystemState;
    ...
    PySystemState engineSys = new PySystemState();
    engineSys.path.append(Py.newString("C:/GMSEC_API/bin/gmsecapi.jar"));
    Py.setSystemState(engineSys);
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

当我使用Jython执行此操作时,它工作正常,python文件可以使用里面的api类jar文件。

When I do this with Jython it works fine and the python files can use the api classes that are inside the jar file.

推荐答案

我能够以这种方式在JavaScript中使用jar的类,但你必须将jar设置为你去运行它时的类路径。我正在寻找一个类似于Jython / Python的解决方案,你可以在Java中设置jar,但我只是创建批处理文件和sh文件并按照这种方式设置(现在看起来更容易)。这是我的解决方案,适用于我。

I am able to use the jar's classes within JavaScript this way, but you have to set the jar to the class path when you go to run it. I was after a solution similar to Jython/Python's where you're able to set the jar inside Java but I'm just going to create batch files and sh files and set it that way (seems easier to do it that way now). Here is my solution that works for me.

编译然后运行:

    cd C:\your\directory\folder\with\the\javascript\and\java\files

    javac -d . ClassSpy.java FileSearch.java HelloWorld.java Main.java Parameters.java Run.java

    java -cp ./;C:\ABCAPI\bin\abcapi.jar hammer.main.Main gui=true input=JavaScriptStatus.js

以上行的评论


  • 编译时,你可以使用 -d。如果你有你的java文件作为您定义的包,它将它们放在包名称的文件夹中。如果还不清楚,请查看这篇文章(我即将离职)如何编译java中的包?

  • when compiling you can use -d . if you have your java files as packages that you defined and it'll put them in folders with your package name. If that's not clear just check out this post (I'm getting off task) How to compile packages in java? .

另一个注意事项是,当我运行它时,我在类路径中添加了两个东西 - cp 代表类路径,之后的任何内容都将添加到当前运行的类路径中。你可以用分号分隔它们。 ./ 将当前目录添加到类路径中,之后我添加的其余内容是我需要的API jar文件的位置。

Another note is that when I ran it I added two things to my class path -cp stands for class path and anything after that will be added to your class path for this current run. You can separate them with a semicolon. ./ adds the current directory to the class path and the rest I added after that was the location of the API's jar file I needed.

hammer.main是类Main的包位置,它是包含启动程序的主函数的类。

hammer.main is the package location of the class Main which is the class containing my main function to start the program.

之后的两个参数特定于我的程序,你可以看到我将告诉Java ScriptEngine读取和执行的JavaScript文件。

The two arguments after that are specific to my program and you can see the JavaScript file I'm going to tell Java ScriptEngine to read and execute.

在JavaScript中使用Jar文件:

现在jar文件位于类路径上并且可见我们可以从该jar文件中获取一个包,然后从包中调用这些类。因此对于下面的示例 abc.foo.pack.name abcapi.jar 内的一个包,以及这两个类我正在使用 ClassFromTheJarFile AnotherClassFromTheJarFile 在该包中。我正在使用一种将包设置为变量的技术,然后在我想要使用的类前面加上包含该包的变量。一些方法是说 importPackage(com.foo.bar)然后你不必在每次实例化它们时为这些类添加前缀但是这种技术对我来说不起作用原因(也许我做错了)。无论如何,下面的代码对我来说没问题,

Now that the jar file is on the class path and visible we can get a package from inside that jar file then call the classes from the package. So for the example below abc.foo.pack.name is a package inside of abcapi.jar and the two classes I'm using ClassFromTheJarFile and AnotherClassFromTheJarFile are in that package. I'm using a technique of setting the package to a variable then prefixing the classes I want to use with the variable containing the package. Another way is to say importPackage(com.foo.bar) then you wont have to prefix the classes every time you instantiate them but this technique doesn't work for me for some reason (perhaps I did something wrong though). At any rate the following code works fine for me,

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

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

    foo.doSomething();

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

    fooSister.doSomthingFromThisClass();

结论:

事实证明我的错误是试图像这样直接导入类,

It turns out my mistake was trying to import the class directly like this,

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

然后我尝试使用它,

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

哪些无效。

我希望这对某人有所帮助,因为我得到了很多评论,比如你知道Java和JavaScript是两个不同的东西。是的,我知道,你有什么意义?

I hope this helps someone because I was getting a lot of remarks like, "You know Java and JavaScript are two different things right". Well yes I do know that, what is your point?

记住:

我没有在浏览器或互联网上使用它。我通过Java ScriptEngine使用它。如果您需要从URL获取jar文件,我发现此链接可能对您有所帮助 http://mozilla-firefox-extension-dev.blogspot.com/2004/11/calling-java-code-in-custom-jars-from。 HTML

I am not using this in a browser or over the internet. I am using this through Java ScriptEngine. If you need to get the jar file from a URL this link I found might help you http://mozilla-firefox-extension-dev.blogspot.com/2004/11/calling-java-code-in-custom-jars-from.html

这篇关于通过Java ScriptEngine在JavaScript中使用jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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