php exec java,php exec()& java发送参数

本文介绍了如何通过PHP执行Java jar文件,并传递参数以及捕获返回结果。在PHP中使用`exec`函数调用Java,需要注意路径设置和错误输出的处理。Java代码接收到参数并打印出来,通过修改PHP代码添加错误处理部分,可以查看到Java执行的错误信息。这是一个关于PHP与Java交互的基础教程。
摘要由CSDN通过智能技术生成

I want to connect to java via php and print the result out in php. Now I want to send a parameter to java and return it to php to see how it works. How can I do this?

I have this code so far:

PHP:

exec("java jar/name.jar", $output);

print_r($output);

Java:

public class Main {

public static void main(String[] args) {

System.out.print(args[0]);

}

}

The result is

Array ( )

解决方案

Do

exec("/full/path/to/java -jar jar/name.jar 2>&1", $output);

print_r($output);

because

your PHP env probably doesn't have java executable defined in the path (and it shouldn't, either)

jar files are executed with java -jar

you'll want 2>&1 to be able to see error messages in the output (redirect from stderr to stdout)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值