使用来自 JavaScript 的参数运行 .jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11822418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a .jar file with parameters from JavaScript
提问by Veni_Vidi_Vici
Possible Duplicate:
Execute another jar in a java program
可能的重复:
在 Java 程序中执行另一个 jar
I have an executable jar file that accepts two parameters: an input file and an output file:
我有一个可执行的 jar 文件,它接受两个参数:一个输入文件和一个输出文件:
java -jar inputFile.txt outputFile.txt
How can I call exactly this from JavaScript?
我怎样才能从 JavaScript 中准确地调用它?
回答by Stephen C
The answer depends on the context.
答案取决于上下文。
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.
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.If your javascript is trusted code running in a browser with a Java plugin, then you maybe able to make a call to
java.lang.System.exec(...)
passing an appropriatejava
command line in the appropriate fashion. You mayalso 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 在 Web 浏览器沙箱中运行,那么答案是您无法运行 JAR 文件。沙箱旨在阻止你做那种事情。
如果您的 javascript 在 node.js 下运行,那么此 SO Q&A 提供了运行命令问题的解决方案: 如何在 javascript 中运行系统命令?. 这可用于使用
java
适当的参数运行命令。如果您的 javascript 是在带有 Java 插件的浏览器中运行的受信任代码,那么您可以调用以适当的方式
java.lang.System.exec(...)
传递适当的java
命令行。您可能还能够创建一个类加载器,阅读JAR文件的清单,提取入口点类,加载它,并调用“主”的方法。
回答by Andrew Thompson
java -jar executable.jar inputFile.txt outputFile.txt
For further details see the java command synopsis:
有关更多详细信息,请参阅java 命令概要:
SYNOPSIS
概要
- java [ options ] class [ argument ... ]
- java [ options ] -jar file.jar [ argument ... ]
- javaw [ options ] class [ argument ... ]
- javaw [ options ] -jar file.jar [ argument ... ]
- java [选项]类[参数...]
- java [选项] -jar file.jar [参数...]
- javaw [选项] 类 [参数 ...]
- javaw [选项] -jar file.jar [参数...]