简体   繁体   English

如何通过PHP中的shell_exec命令运行Java程序?

[英]How to run Java Program through shell_exec command in PHP?

I have a PHP Project with the name itext . 我有一个名为itextPHP Project Inside this folder I have a workspace folder. 在此文件夹中,我有一个workspace文件夹。 So the Path to the Java Class file is like this: 因此,Java类文件的路径如下所示:

itext/workspace/sampleproject/checkFonts.java

I have an index.php file in itext folder. 我在itext文件夹中有一个index.php文件。 How can I execute the shell_exec command in the index.php for the java file located in the above path? 如何在上述路径中的java文件的index.php中执行shell_exec命令?

UPDATE: 更新:

As per the answer I tried out stuffs and executed the below code and it works but I am getting the below error: 根据答案,我尝试了一些东西并执行了以下代码,它可以正常工作,但出现以下错误:

$output = array();
exec('java workspace/itext/src/itext/CheckFonts 2>&1',$output);
print_r($output);

Error: 错误:

Array ( [0] => Error: Could not find or load main class workspace.itext.src.itext.CheckFonts ) 数组([0] =>错误:无法找到或加载主类workspace.itext.src.itext.CheckFonts)

You can specify any path relative to the file your code is running from. 您可以指定相对于代码运行文件的任何路径。 So from your index.php the path would be workspace/sampleproject 所以从您的index.php路径将是workspace/sampleproject

If you want to use the full explicit directory you can use realpath() function: 如果要使用完整的显式目录,可以使用realpath()函数:

realpath('workspace/sampleproject');

Here's a guide to running Java at the command line for windows environment: http://www.skylit.com/javamethods/faqs/javaindos.html 这是在Windows环境的命令行上运行Java的指南: http : //www.skylit.com/javamethods/faqs/javaindos.html

Your example code might be: 您的示例代码可能是:

$javaPath = realpath('workspace/sampleproject');
$output = shell_exec('C:\ProgramData\Oracle\Java\javapath\java.exe '
                     . $javaPath . DIRECTORY_SEPARATOR . 'checkFonts');`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM