从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.

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.