admin管理员组文章数量:1316974
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.
UPDATE:
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.
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.
UPDATE:
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.
Share Improve this question edited Jul 5, 2014 at 0:44 Kyle Bridenstine asked Jul 4, 2014 at 19:06 Kyle BridenstineKyle Bridenstine 6,39312 gold badges67 silver badges105 bronze badges 3-
define
import
with regard to what you tried and definedoesn't work
– charlietfl Commented Jul 4, 2014 at 19:17 - 1 Where the heck are you running the javascript then? Node.js? Through Rhino? You need to be specific. – Gimby Commented Jul 4, 2014 at 19:19
- @Gimby Please see the update above despite the question now being answered. – Kyle Bridenstine Commented Jul 6, 2014 at 18:44
1 Answer
Reset to default 6I 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
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;
So only import the package and not the classes. To clarify you do not need to use the Java *
.
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./2004/11/calling-java-code-in-custom-jars-from.html
本文标签: Can JavaScript use classes from a jar file (Non Internet Use)Stack Overflow
版权声明:本文标题:Can JavaScript use classes from a jar file (Non Internet Use) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742020542a2414508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论