I want to call a java program and fetch it's output in stdout. I followed the suggestions in stackoverflow. But it doesn't work.
I have add the class file to my CLASSPATH. And I can execute the command in cmd correctly as follows:
In my PHP file I call this program by
exec("java Hello", $output);
print_r($output);
It yields nothing but:
Array()
What is the problem? How can I fix this?
ps: Hello is a demo program, actually the program I want to call is much more complicated which might take 2 or more seconds in my machine(i5 4G).
I would recommend using Java/PHP Bridge found here: http://php-java-bridge.sourceforge.net/pjb/
It's quite easy to install and works very well.
Also, I recommend using the following link to download it. (it's the same one as the link in downloads->documentation)
http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.2.1/php-java-bridge_6.2.1_documentation.zip/download
The file is JavaBridge.war. You'll probably want to use Tomcat for the Java EE container. Once Tomcat is set up, you just put this file in the webapps folder and it's installed.
If you want to regularly use java classes in PHP this is the best method I know of and I have tried a lot of them. Resin also worked, but it didn't play nice with my mail server.
Try this:
exec('java -cp .:/path/to/folder/of/your/file Hello 2>&1', $output);
print_r($output);
the 2>&1 need to display errors.
Well, it yields array right? so instead print_r($output) try print($output[0]), that outputting 'Hello World' on my console :D
try pipe
$command = 'java Hello';
$descriptorspec = array(
1 => array(
'pipe', 'w'
)
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
exit("failed to create process");
}
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) === 0) {
print_r($content);
}else{
exit("failed to execute Hello");
}
Related
im trying to run a java file through php and shell_exec() is behaving in a strange way.
<?php
shell_exec("javac Driver.java");
echo shell_exec("java Driver");
?>
This code causes the contents of the java file to be displayed in my web browser and i have no idea why as i am new to php. Any insight would be appreciated.
It could be either because there is a problem during compilation and so no program to execute or maybe because of redirection of output.
To resolve the issue.
Step 1 : Compile the java program from commandline and make sure java program-name command is giving desired output. Also use absolute path to java file whenever required
Step 2 : Then if that is correct you should check if redirection of output is correct. system.out.println may not be using stderr . Try adding adding 2>&1 after your command.
Step 3 : Check your classpath.
Please look at the link below. It has a solution to same issue you are facing.
Running a Java File from PHP
I want to call a java program and fetch it's output in stdout. I followed the suggestions in stackoverflow. But it doesn't work.
I have add the class file to my CLASSPATH. And I can execute the command in cmd correctly as follows:
In my PHP file I call this program by
exec("java Hello", $output);
print_r($output);
It yields nothing but:
Array()
What is the problem? How can I fix this?
ps: Hello is a demo program, actually the program I want to call is much more complicated which might take 2 or more seconds in my machine(i5 4G).
I would recommend using Java/PHP Bridge found here: http://php-java-bridge.sourceforge.net/pjb/
It's quite easy to install and works very well.
Also, I recommend using the following link to download it. (it's the same one as the link in downloads->documentation)
http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.2.1/php-java-bridge_6.2.1_documentation.zip/download
The file is JavaBridge.war. You'll probably want to use Tomcat for the Java EE container. Once Tomcat is set up, you just put this file in the webapps folder and it's installed.
If you want to regularly use java classes in PHP this is the best method I know of and I have tried a lot of them. Resin also worked, but it didn't play nice with my mail server.
Try this:
exec('java -cp .:/path/to/folder/of/your/file Hello 2>&1', $output);
print_r($output);
the 2>&1 need to display errors.
Well, it yields array right? so instead print_r($output) try print($output[0]), that outputting 'Hello World' on my console :D
try pipe
$command = 'java Hello';
$descriptorspec = array(
1 => array(
'pipe', 'w'
)
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
exit("failed to create process");
}
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) === 0) {
print_r($content);
}else{
exit("failed to execute Hello");
}
I am having troubles with the passthru() command when executing Java programs. More specifically with setting the LD_LIBRARY_PATH using passthru.
I tried:
setting the .so file path directly like this:
passthru("java
-Djava.library.path='.:/path/to/directory/of/.so/object' " HelloWorld);
writing a shell script which sets the LD_LIBRARY_PATH,then executing the shell script using passthru.
acessing the /etc/profile file and adding the "export LD_LIBRARY_PATH..." line in the document.
These are just few solutions I found online which I thought would work, but none worked. I believed the problems come from concatenation stuff in the passthru command as I saw on several sites stuff like that:
$command = 'export LD_LIBRARY_PATH="' . $path_to_library_dir .'"; ' .
$path_to_binary; passthru($command);
which I also tried.But I dont know what am doing wrong.
Please, can anyone help?
Thank you!
EDIT 1
Using #lexmihaylov suggestion:
Hi,
Thank you for replying.
Actually I get an "java.lang.UnsatisfiedLinkError:/usr/lib/libjpcap.so". So that's why I am trying to set up the LD_LIBRARY_PATH to my .so object.
From your answer I tried:
$Path_to_library_dir = '/usr/lib64/jpcap';
$Path_to_binary = '/usr/lib64/jpcap/libjpcap.so';
$command = 'LD_LIBRARY_PATH="' . $Path_to_library_dir .'" '.$Path_to_binary;
passthru($command);
$java_execution = "java myjavaprogram 2>&1";
echo passthru($java_execution,$output);
echo $output;
I still get the same java.lang.UnsatisfiedLinkError. But if I set the LD_LIBRARY_PATH directly in my terminal, my java program works fine when executed via terminal.
Thank you for helping!
It should work with you second example but remove the export and ';' from the shell command. Just try this and tell me what happens:
$command = 'LD_LIBRARY_PATH="' . $path_to_library_dir .'" ' . $path_to_binary;
passthru($command);
first I have to tell you, that i'm a french guy, so i can make some mistake with my english ;-)
Here is my problem : I want to use a java processor to transform a XML file.
I made a shell script wich is working well.
But when I execute the shell script from PHP it doesn't work ...
// I tried this
$resultat = shell_exec("sh ".$chemin."script.sh");
// And after this
$resultat = shell_exec("java -jar ". $jar ." -s:".$source." -xsl:".$xslt);
The file "script.sh" contains this :
JAR='lib/saxon/saxon9he.jar'
SOURCE='temp/fichier_xml.xml'
RESULT="temp/output.xml"
XSLT="xml_to_xml.xsl"
java -jar $JAR -s:$SOURCE -xsl:$XSLT
I think that's a problem with JAVA ... But I can not resolve this !!
If you have an idea to help me,
Thanks
Try using the script in backticks i.e. ``
shell_exec('cd ' . $chemin . ' & sh script.sh');
Go to your $chemin dir
Then run your script, as java need correct pathes
I am trying to run a Jar file in the backend of my php code.But I am not getting the desired output to it.There is a jar file which runs in the background and returns the Page Rank of any of the keyword and Domain given to it.
I am attaching the code,please suggest me any solution to it,because when I run it on the terminal,it is giving correct output.
Here is the Code :
<?php
set_time_limit(0);
function returnJarPath()
{
$jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";
return $jarPath;
}
$jar = returnJarPath();
$command = "java -jar $jar aspdotnet/microsoft.com";//Passing the Argument to the Jar file.
$shellOutput = shell_exec($command);
print "The Shell Output is : " ; var_dump($shellOutput);print "<br />";
exec($command,$executeCommmand);
print "The Exec returns the value : " ; var_dump($executeCommmand);print "<br />";
passthru($command,$passthruCommand);
print "The Passthru returns the value : " . $passthruCommand. "<br />";
?>
I just checked apache's error log and the last error I found was :
sh: java: command not found
But as I have already said,I have been using the same command through SSH to run the Java command.So there's no such possibility of not having JAVA installed on the server.Please help me out of this mess...
If the jar file writes to standard output you can use exec.
Here is an example how I use it:
may be first: exec("cd jar dir"); // if jar fine needs to be executed from the same dir
$output = exec("/usr/bin/java -jar $jar aspdotnet/microsoft.com");
But as you say:
sh: java: command not found
It means the there is no path alias to java from php. Just use the full java path to the executable /usr/bin/java.
Given you are calling java. My bet is the output is being displayed in the Java Console, and not in the shell, where PHP could pull the text information.
How to solve this dilemma?
Well you could write the results to a file, if you have the java source to modify, and then read that file through php to get the results. The possibility of a collision here would be pretty good. The other option is to have Java connect to your MySQL database (if you had one) and then run the java then query the database for the response. Of course, you would need to pass Java a way for it to input the data to insert a record you could identify (a hash of some sort), I have never done that in Java, just a theory of how you might be able to do it.
Update
You may want to try the standard output as suggested by darko petreski as another option as well.
If the PHP code is to be executed in a server (and not via command line) the user that runs the java executable is www-data, not you. In that case make sure that www-data has the permissions to read the jar file and to execute the java executable
The first thing that I would check/change is the line in the function where you are building the $jarPath variable from this:
$jarPath = $_SERVER['DOCUMENT_ROOT'] . "myFolder/tools_new/includes/Rank.jar";
to this:
$jarPath = $_SERVER['DOCUMENT_ROOT'] . "/myFolder/tools_new/includes/Rank.jar";
The trailing slash may not be present in $_SERVER['DOCUMENT_ROOT'] which could cause issues.
I am assuming that when you say it runs from the console, you are running the java command like so:
$ java -jar /rest/of/path/myFolder/tools_new/includes/Rank.jar aspdotnet/microsoft.com
I would ensure that you include the path to the java binary in the $command variable like so...
$command = "/path/to/java -jar $jar aspdotnet/microsoft.com";
The user that owns the web server process may not have a $PATH variable that includes the path to the Java binary.