开发者

Executing Java JAR from PHP will not work

开发者 https://www.devze.com 2022-12-31 03:41 出处:网络
I have a java program that generates an HTML file. The Java program takes two input parameters: file1 and file2, the output file is specified by the \"--file=\".

I have a java program that generates an HTML file. The Java program takes two input parameters: file1 and file2, the output file is specified by the "--file=".

When executed from the command line (UNIX, Mac OS 10.6.2) it looks like this:

"java -jar program.jar http://my.testsite.com/test1.html http://my.testsite.com/test2.html --file=/path/to/jar/new_file_1274119954.html

Executing the program form the command line generates the file "new_file_TIEMSTAMP.html"

Now I want execute this same line form PHP so I created the following script:



$file1 = 'http://my.testsite.com/test1.html';
$file2 = 'http://my.testsite.com/test2.html';
$newFile = '/path/to/jar/new_file_'.time().'.html';


system("java -jar program.jar $file1 $file2 --file=$newFile");

$handle = fopen($newFile, "r");
$output = fread($handle, fil开发者_StackOverflowesize($newFile));

echo "$output";

Like you can see, I am not really interested in the console output, but rather in the file that is generated by the program.

I have also use the following calls to execute the console command but neither of them works when executing this PHP script from my browser.

  • system()
  • exec()
  • shell_exec()

I am not sure if there is some special setting that I need to enable in my php.ini, or is something I am doing wrong, but I am not sure what to do now.

Your help is appreciated

PD:

One error message I do get when using the system call is the following: javax.xml.transform.TransformerException: org.xml.sax.SAXException: setResult() must be called prior to startDocument().


Well, the Java exception tells us there is an XML exception in there, so maybe start with seeing if your source file is valid? It's hard to tell from here what the Java app does.

Also, I'd have a look to see if apache or the PHP user has permission to execute the Java app, and create files.

Good luck.


I really don't know much about PHP, but is it possible that when you do the system("java -jar program.jar $file1 $file2 --file=$newFile") call, it isn't evaluating the value of $file1 and $file2, so you are asking it to find a file named $file1, instead of test1.html ?

Instead, I think you want something like

$cmd = "java -jar program.jar " . $file1 . " " . $file2 . "--file=" . $newFile;
system($cmd);


Assuming you ran into the same problem I did, using DaisyDiff, you need to create the directory before a file can be placed into it. It will not create the needed files by itself.

Hope this will help!

0

精彩评论

暂无评论...
验证码 换一张
取 消