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 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行