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
Related
I am doing something about getting execution logs form a java program. But I just achieve this when it's a jar file using command like java -javaagent:agent1.jar -jar MyProgram.jar . But if there is a software which don't need using java -jar to open , how can I use my agent1.jar to get its execution data ? For example, I made a game by java ,and I open it by opening the file MyGame.exe ,which mean that I can't use javaagent:agent.jar to instrument and get logs.
Are there any ways to solve this problem or any other tools can give me a reference?
Scenario : I have a Referee.jar program which I got from somewhere (making a point that I don't know to change that code). Also, I have 2 python files which I've written.
Currently : The JAR file has to be executed first in the terminal with "java -jar referee.jar" and then "python 1.py" and "python 2.py" have to be typed into the following lines.
Requirement : I want to make a shell script which will do that and store the output into a file called 'out.txt'
Found the answer here.
Automatically answer to input prompt in windows batch
Just had to echo the file names and redirect it to the command.
My sincere apologies for not checking this before posting the question.
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");
}
My website needs PHP to run a Java program in the background. So, PHP issues exec() method to let Java do all the work. The implementation works fine on Windows, but completely failed on Ubuntu. While exec() doesn't work, a stand-alone test with console works just fine.
I've setup a test.php to narrow down the problem:
<?php
$output = exec("java -cp ~/path/to/java/class/file/folder Hello 2>&1");
//$output = exec("whoami");
echo $output;
?>
The Hello.java is simply:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
By running test.php on localhost, it shows:
Error: Could not find or load main class Hello
I tried to narrow down the cause of the error, and my thought went like this:
exec() itself is problematic:
unlikely, since whoami prints out apache-user as expected.
what the error message means:
I searched about this error. Post like this one talks about it is caused by the absence of classpath. It's not the case for me either, because in console it works. So the error message means nothing (does it?)
user/group permission:
Is it possible that apache-user is not permitted to run the class file?
I checked, and found the permission code of Hello.class to be rw-r--r--, owned by apache-user:webmasters.
But, even if no one has x permission of the file, in console I can still run it (using my own user).
I'm not sure about the situation here. But my understanding is that by running java program, it is really JVM executing it (or something else); so the permission of Hello.class doesn't matter.
I found another post has a similar situation. But its solution - specifying full path to Java bin /usr/bin/java - doesn't work for me...
What is causing the error?
Can anyone help? Detailed solution is appreciated! I'm a newbie #_#
Many thanks!!!
Have you tried java -cp /path/to/folder/containing/class/file Hello 2>&1? It appears that the class file itself should not be the classpath. It should be in the classpath. If this were a .jar file, on the other hand, then you would provide the filename in the classpath.
I've solved the problem... It's quite unexpected.
I changed the classpath.
Previously it's something like ~/myproject/to/java/class/file/folder.
And I changed it to /home/myuser/myproject/to/java/class/file/folder.
But I completely don't understand why ~ notation doesn't work with exec().
Give the path and Hello.java file free.
Test the rights for the apache user with:
sudo -u webmasters java -cp /path/to/java/class/file/folder Hello
chmod a+r Hello.class
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.