dongping1689 2011-05-13 04:13
浏览 74

从PHP中运行Java程序的问题

I'm writing a PHP script that basically calls a Java program with a string, has Java create a file whose name is that string, and returning that file. Also, it's all 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);

Only problem is I get this problem: Error occurred during initialization of VM java.lang.Error: Properties init: Could not determine current working directory.

I have tried using exec. The script completes but the java program doesn't run.

  • 写回答

1条回答 默认 最新

  • doucu7525 2011-05-13 04:57
    关注

    Try using the OS shell (bash on *nix and cmd on Windows) as it probably does a better job of configuring the environment correctly. Try something like this:

    $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);
    

    EDIT: on Ubuntu, might want to explicitly use /bin/bash, since they insist on doing things like linking sh to dash (which among other truly annoying things doesn't natively support pushd and popd, breaking shell calls that worked perfectly for years and years...). If you still get working directory problems you can put a pushd <required directory> before the java call on *nix machines (not sure what the Windows syntax would be: might want to make a cmd script that takes care of all the working directory bits and just call that).

    EDIT: ignore my rant at the end of the comment above. I did this a couple of years ago, and had to use exec:

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

    My configuration included the full path to the Java runtime and the directory that the jar files were in (this was using Code Igniter) and the output was saved into the $r array. Try something like that and let me know if it works.

    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度