1

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.

2
  • 1
    Try using exec with output redirection for the command to file, like java MCS ... > outputlog.txt and post the result. Commented May 13, 2011 at 4:17
  • Properties init: Could not determine current working directory. happens in some JVM17 when there is no such directory and a user is running from inside of it. Ironically after doing a step back cd .. a user appears in .Trash directory there and if merely to repeat the last command in log sometimes there is one such as the application is started by user_name in /Users/user_name/.Trash. Commented Nov 13, 2024 at 16:16

1 Answer 1

1

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") . '\requirements.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.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately none of this worked. I'm not really sure what you mean by using pushd, either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.