如何运行Java程序并在PHP中获取输出? [英] How to run Java program and get output in PHP?

查看:67
本文介绍了如何运行Java程序并在PHP中获取输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行类似的程序(在myProgram.sh中):

I'd like to run something like (in myProgram.sh):

java -cp whatever.jar com.my.program $1

在PHP中并读取输出.

within PHP and read the output.

到目前为止,我有类似的东西:

So far I have something like:

$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);

但是我真正想要的是能够在PHP脚本中获取java程序的输出,而不仅仅是将其作为另一个线程来执行.

But what I'd really like is to be able to get the output of the java program within the PHP script and not just execute it as another thread.

这怎么办?

推荐答案

关键是在shell_exec中类路径必须是绝对的 PHP脚本.

The key is that the classpaths need to be absolute within the shell_exec PHP script.

或者至少这是我可以使其正常工作的唯一方法.基本上,几乎不可能从一个环境到另一个环境都知道php脚本正在运行JVM的相对目录是什么.

Or at least that's the only way I could get it to correctly work. Basically it's almost impossible to tell from environment to environment what the relative directory is that the php script is running the JVM.

同样,它有助于放置Java的绝对路径位置,例如usr/.../bin/java

As well, it helped to put the absolute path location for java, such as usr/.../bin/java

这篇关于如何运行Java程序并在PHP中获取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆