从PHP中运行Java程序的问题

时间:2011-05-13 04:13:46

标签: java php

我正在编写一个基本上用字符串调用Java程序的PHP脚本,Java创建一个名称为该字符串的文件,然后返回该文件。而且,这都是midi。

$output_file = mt_rand(10000000,99999999) . ".mid";
system("java MCS " . $_FILES["file"]["tmp_name"] . " " . $output_file . " " . escapeshellarg($_POST['message']));
header('Content-type: audio/midi');
header('Content-Disposition: attachment; filename="output.mid"');
readfile($output_file);
unlink($output_file);

唯一的问题是我遇到此问题:VM初始化期间发生错误java.lang.Error:属性init:无法确定当前工作目录。

我尝试过使用exec。脚本完成但java程序没有运行。

1 个答案:

答案 0 :(得分:1)

尝试在Windows上使用操作系统shell(bash和Windows上的cmd),因为它可能会更好地正确配置环境。尝试这样的事情:

$output_file = mt_rand(10000000,99999999) . ".mid";
$platformshell = "/bin/sh -c "; // on windows use "cmd /c "
system($platformshell . "\"java MCS " . $_FILES["file"]["tmp_name"] . " " . $output_file . " " . escapeshellarg($_POST['message']) . "\"");
header('Content-type: audio/midi');
header('Content-Disposition: attachment; filename="output.mid"');
readfile($output_file);
unlink($output_file);
编辑:在Ubuntu上,可能希望明确使用/bin/bash,因为他们坚持要做一些事情,例如将sh链接到dash(其他真正烦人的东西本身不支持 pushd popd ,破坏了多年来一直运行良好的shell调用......)。如果仍然遇到工作目录问题,可以在* nix机器上的 java 调用之前放置pushd <required directory>(不确定Windows语法是什么:可能想要创建 cmd 脚本,负责处理所有工作目录位并调用它。

编辑:在上面评论的末尾忽略我的咆哮。几年前我这样做了,不得不使用exec

$r = array();
    exec($this->config->item("java_path") . ' -cp ' . $this->config->item("java_base") . '\databasebackups.jar;' . $this->config->item("java_base") . '\requirements.jar ' . $startdate . $enddate . ' com.blah.blah.LastTransactionReport --user sa --password ' . $this->config->item("ms_password") . ' --dburl ' . $this->config->item("ms_url"), $r);

我的配置包括Java运行时的完整路径和jar文件所在的目录(这是使用Code Igniter),输出保存到$r数组中。尝试类似的东西,让我知道它是否有效。