如何在 Java 应用程序中通过 JavaScript 启动程序?
我在从 Java 应用程序中启动 JavaScript(文件)时遇到了一些问题。我知道如果我将应用程序放入 example.js 中,则可以通过一个简单的脚本启动该应用程序。实际调用如下:
new ActiveXObject("MyApp.Application");
如前所述,通过双击脚本文件启动是没有问题的,但是如果我通过 javax.script 包尝试:
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code from String
engine.eval("new ActiveXObject(\"MyApp.Application\");");
如果出现以下异常:
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "ActiveXObject" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at com.ime.actia.testing.ScriptTest.main(ScriptTest.java:13)
Microsoft Windows Script is displayed in the current version 。我知道,ActiveX 是 IE 特定的,但我不想在 IE 中/通过 IE 启动应用程序。有人知道如何启动该程序吗?
谢谢大家! ^^
编辑:感谢您的回答!由于我无法从 JVM 中获取 ActiveXObject,是否有另一种方法可以通过 JScript 启动外部应用程序?我不在乎它是否是 ActiveXObject。
i have a little problem with starting a JavaScript (-file) from within a Java Application. I know the Application is startable through a simple Script if i put it into a sample.js. The actual call is as follows:
new ActiveXObject("MyApp.Application");
As said, the start via double click on the script file is no problem, but if i try it through the javax.script package:
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// evaluate JavaScript code from String
engine.eval("new ActiveXObject(\"MyApp.Application\");");
if get the following exception:
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "ActiveXObject" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at com.ime.actia.testing.ScriptTest.main(ScriptTest.java:13)
Microsoft Windows Script is installed in the actual version. I know, ActiveX is IE specific, but i don't wanna start the App in/through the IE anyway. Has anybody an idea, how to start the program?
Thanks everyone! ^^
EDIT: Thanks for answers by now! Since I'm not able to get an ActiveXObject from within a JVM, is there another way to start an external application through JScript? I don't care, if it's an ActiveXObject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浏览器中的JavaScript代码可以使用环境(即浏览器)提供的宿主对象。 ActiveXObject 就是 IE 提供的此类对象之一。从您的异常跟踪来看,该对象在您的环境中似乎不可用。
JavaScript code in browser can use host objects supplied by the environment (i.e. the browser). ActiveXObject is one such object provided by IE. From your exception trace, it looks like that object is not available in your environment.
并非所有可用于浏览器中运行的 Javascript 的对象也可用于 Java 程序 (JVM) 内运行的 Javascript。而且,这就是为什么它抱怨“ActiveXObject”未定义。
Not all objects which are available to Javascript running in browser will be available to Javascript running inside your Java program (JVM). And, that is why it complains 'ActiveXObject' is not defined.