使用JavaScript中的参数运行.jar文件 [英] Run a .jar file with parameters from JavaScript
问题描述
可能重复:
在java程序中执行另一个jar
我有一个可执行jar文件,它接受两个参数:输入文件和输出文件:
I have an executable jar file that accepts two parameters: an input file and an output file:
java -jar inputFile.txt outputFile.txt
如何从JavaScript中调用这个?
How can I call exactly this from JavaScript?
推荐答案
答案取决于具体情况。
-
如果你的话javascript正在Web浏览器沙箱中运行,那么答案是您无法运行JAR文件。沙盒旨在阻止你做这种事情。
If your javascript is running in a web browser sandbox, then the answer is that you can't run a JAR file. The sandbox is designed to stop you doing that kind of thing.
如果你的javascript在node.js下运行,那么这个SO Q& A提供了一个解决方案运行命令的问题:
如何做我在javascript中运行系统命令?。这可用于运行带有相应args的 java
命令。
If your javascript is running under node.js, then this SO Q&A offers a solution to the problem of running a command:
How do I run the system commands in javascript?. This can be used to run the java
command with the appropriate args.
如果您的javascript是可信的在带有Java插件的浏览器中运行的代码,然后您可以能够调用 java.lang.System.exec(...)
以适当的方式传递适当的 java
命令行。你可能也能够创建一个类加载器,读取JAR文件的清单,提取入口点类,加载它,并调用main方法。
If your javascript is trusted code running in a browser with a Java plugin, then you may be able to make a call to java.lang.System.exec(...)
passing an appropriate java
command line in the appropriate fashion. You may also be able to create a classloader, read the JAR file's manifest, extract the entry point class, load it, and call the "main" method.
这篇关于使用JavaScript中的参数运行.jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!