java 如何使用传递给main方法的参数从php调用可执行jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16766464/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:57:01  来源:igfitidea点击:

How to call an executable jar from php with an argument passed to main method

javaphpjarexecutable-jar

提问by user264953

I have created an executable jar and it works fine. For the testing purpose, I have used a dummy variable in the main method.

我创建了一个可执行的 jar,它工作正常。出于测试目的,我在 main 方法中使用了一个虚拟变量。

Instead, I need to pass a string variable, while calling this executable jar from php.

相反,我需要传递一个字符串变量,同时从 php 调用这个可执行的 jar。

In short, I need to:

简而言之,我需要:

  1. Call the executable jar from PHP
  2. While calling the jar, I need to pass a string variable from PHP to the main method of the class inside executable jar.
  1. 从 PHP 调用可执行 jar
  2. 在调用 jar 时,我需要将一个字符串变量从 PHP 传递到可执行 jar 中的类的 main 方法。

How can I accomplish the aforesaid?

我怎样才能完成上述操作?

回答by Sumit Singh

shell_exec("java -jar your_JAR.jar arg1 arg2");

you can also execute Java at all by using execinstead of shell_execute, giving it $outputand & $return_varvariables (see the linked manual page) and seeing what they contain after the call.

您还可以通过使用exec而不是shell_execute,给它$output和 &$return_var变量(参见链接的手册页)并在调用后查看它们包含的内容来完全执行 Java 。