java - 在 PHP 中运行 java ".class"

标签 java php wampserver

嗨,我是 PHP 编程新手。我使用 WAMP 服务器 -> Apache 版本 2.4.4 和 PHP 版本 5.4.16

我想从php脚本执行java程序,这是我的java程序

   file : test.java
    import java.io.*; 
    public class test {
        public static void main(String args[]) {
            System.out.println("Hello World");
        }
    }

我用过

exec("javac test.java");
exec(java test);

这没有用。于是我尝试将命令代码放入“runfile.bat”

javac test.java > error.txt
java test > output.txt

当我通过左键单击两次执行runfile.bat时,它运行完美,并且output.txt中有文本“Hello World”

然后我尝试使用 PHP:

exec('start /B /C runfile.bat');

...而output.txt 中没有任何内容。

最佳答案

PHP 的 exec() 肯定可以。

<?php
exec('java test', $output, $return_var);

// `$output` captures the output of command executed
print_r($output);

// Generally `$return_var` becomes 0 if the command was successful
if (0 == $return_var) {
    // Command successfull
}
?>
PS: 确保您的 Java 类路径正确。 始终寻找PHP Manual ——这是我见过的最好的手册。

关于java - 在 PHP 中运行 java ".class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23221838/

相关文章:

php - fopen(file,w+) 在我检查文件是否被 flock() 锁定之前截断文件

用于从 mysql 中提取客户数据的 PHP 搜索脚本

php - curl 错误 8 : Invalid Content-Length: value

apache - WAMP 2.5 (Apache 2.4.9) 允许从 LAN 上的所有计算机进行访问

php - 使用 WampServer (Win7/64) 将哪个 PHP 文件添加到我的路径

java - Mysql 5.5 与 hibernate 4 order by 不一致

java - 当socket io.read()无法获取任何数据时,java线程的状态是什么

java - 我什么时候应该将代码分成单独的类?

php - Ajax 和 PHP 表单 : window still refreshing

java - 使用 JNA 在 Clojure 中按值获取和传递结构