在 windows 上通过 PHP 启动 java 可执行文件

Start java executable via PHP on windows

我尝试通过 PHP 启动一个 java 文件。我创建了一个名为 start_selenium_server.bat.

的 .bat 文件

这是内容:java -jar -Dwebdriver.gecko.driver="drivers/geckodriver.exe" selenium-server-standalone-3.4.0.jar -port 4444

文件夹drivers和.bat文件在同一个文件夹,在我项目的根目录下

文件selenium-server-standalone-3.4.0.jar也在同一目录下。

这是我尝试启动它的方式:

    $out = array();
    $outvar = "";
    exec("start_selenium_server.bat", $out, $outvar);

    if ($outvar == 0) {
        return redirect()->back()->with("message", "Selenium Server wurde gestartet.")
                                 ->with("status", "success");
    } else {
        return redirect()->back()->with("message", "Selenium Server konnte nicht gestartet werden!")
                                 ->with("status", "error");
    }

但是网站永远加载,什么也没有发生。

编辑: 我注意到 cmd.execonhost.exe 任务正在任务管理器中产生。

这对我来说似乎合乎逻辑,服务器启动,然后 运行s,并没有结束。

您可能只是想使用 &.

作为后台进程
exec("/path/to/script &");

当然,您正在使用 windows,这给 table 带来了一系列麻烦。我给你的解决方案是针对基于 Linux 的系统(默认的预期配置),所以你必须找出如何获得 windows executable 到 运行 在后台。

UPDATE

我刚刚看了一下,发现了这个:

START /B program

所以我认为你需要的是:

exec('START /B start_selenium_server.bat');

当然,不利的一面是您的系统现在耦合到 Windows。

Another update

这应该适用于 Linux 和 Windows。请注意,我们不在 Windows 中使用 exec,所以请尝试一下!

if (substr(php_uname(), 0, 7) == "Windows"){ 
     pclose(popen("start /B ". $cmd, "r"));  
 } 
 else { 
     exec($cmd . " > /dev/null &");   
 }

我用proc_open解决了。

    $descriptorspec = array(
       0 => array("file", "tmp/stdin.log", "a"),
       1 => array("file", "tmp/stdout.log", "a"),
       2 => array("file", "tmp/stderr.log", "a")
    );

    $process = proc_open ('START /B start_selenium_server.bat', $descriptorspec, $pipes);

   if (is_resource($process)) {

        $response = proc_close($process);


        if ($response == 0) {
            return redirect()->back()->with("message", "Kommando wurde ausgeführt.")
                                     ->with("status", "success");
        } else {
            return redirect()->back()->with("message", "Error !")
                                     ->with("status", "warning")
        }

    }

服务器现在启动,但我需要改进这段代码以检查它是否成功并显示给用户。