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.
Related
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.
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 have created a jar file , it reads from standard input and print them out to the standard output.
Below is how you can use the app:
cat /ephemeral/test/input | java -jar /ephemeral/test/Hello.jar >> /ephemeral/test/output
I am wondering is it possible to add my Hello.jar to be a Linux Command, let's say -hello-:
So next time I could just do something like:
hello /ephemeral/test/input /ephemeral/test/output
I am not that much familiar with Linux Soft Link and Java, so any help or information would be appricated!
UPDATE:
thanks for reno's inspiring answer, I put the cat ... | .. >> .. into a shell script hello.sh.
Now I can do something like:
./hello.sh /inputpath/input outputpath/output
(Note: you should not use $0, $0 is actually the name the file itself instead of input arguments)
Now, the last question is how could you access the shell script EVERYWHERE, like how you call basic shell cmds: ls..cat..echo..
I have read a tutorial described exactly what I want(reach to the end of the tutorial directly):
I did add the path of the directory where the hello.sh exists into the path variable.
I created a bin folder which is like mkdir /home/ubuntu/bin and put my shell script there.
However, I still have problem do what the author described:
hello input output (in my case)
Does anyone know what is going wrong here?
you can write a shell script say test.sh and copy your code to test.sh
!/bin/bash
cat $0 | java -jar /ephemeral/test/Hello.jar >> $1
and execute the script by passing your input and output file name something like this:
test.sh /ephemeral/test/input /ephemeral/test/output
The way to do this is to write a shell script (usually hello.sh) that contains the necessary java -jar command.
I have created one java program on my Linux system which indents and formats the given file. I want to make that program work like a command in Linux which will take file names and other options as arguments and then will produce the output. I can do this with a C program by just copying the compiled executable in /bin folder but I don't know how to do it with java.
Sample script that can might further help-
#!/bin/bash
#Set whatever number of arguments you expect for the Java jar you have
ARGS_EXPECTED=3
if [ $# -ne $ARGS_EXPECTED ]
then
echo "[$HOSTNAME]: Usage: `basename $0` filename arg1 arg2"
exit 1
fi
java -cp yourfile.jar com.yourpkg.Driver $1 $2 $3
Save the above content to a file, say test.sh
and use the command to give an executable permission chmod +x test.sh
run like ./test.sh filename arg1 arg2 from current directory where test.sh is
I thing this can be useful for your case: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/binfmt_misc.txt?id=HEAD
You can simply run a Java class file or jar file with "java" command from command line. Example:
java -jar yourprogram.jar argument1 argument2
If you save this line in a text file saved for example as "script.sh" and then give it the permission for execution you can run it double clicking or from terminal typing ./script.sh in the same folder containing the file script.sh.
You can also produce scripts that use arguments with $1 $2 etc. avoiding the need of editing file.
http://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-parameter-to-shell-script-254396/
You can use named parameters, too.
You can also produce a C program for a new command like you suggested that run the "java" command. In this case you can introduce arguments directly from terminal and pass them to java command in the C source.
As others have pointed out it is probably best to use a small shell script to run the Java application. There are several open source products that will help you wrap your Java code to produce a runnable (set of) .jar(s).
If you have correctly separated your business logic from your interface (as you should) then it is probably best if your Java application parses the parameters given on the command line interface. To do this create a separate class for parsing such parameters and calling the classes making up the business logic. Of course this will lead quickly - if not immediately - in writing a parser for Linux like CLI parameters. When this happens you may wish to consider the Apache Commons CLI project.
If you don't want to use any wrapping application/runtime, my method is generally pointing to all the class file containers in the classpath and directly pointing to the class containing the static main method:
java -cp "path_to_jar;path_to_class_folder;etc" "nl.owlstead.stackoverflow.LinuxMain"
I call a Java function using PHP. The code is:
exec('pushd d:\xampp\htdocs\file_excecute\class & java Autoingestion username password id Sales Daily Summary 20120902',$output,$return);
This code worked on a Windows machine but it is not working on a Linux server. The code is:
exec('pushd \var\www\domainname.com\itune_report\class & java Autoingestion username password id Sales Weekly Summary 20120901',$output,$return);
You are using the wrong kind of slash as a field separator, but that may not be your only problem.
The output of the command appears in $output, since you use the exec(command, output, return) form.
However, this only gives you stdout. The shell will send error messages to stderr.
Unfortunately there isn't a version of exec() that reads stderr.
You can merge both outputs to $output by adding 2>&1 at the end of your shell command:
exec("mycommand 2>&1", $output, $return);
Look at $output, and you will either find the output of your successful command or error messages which you can use to work out why it didn't work.
If you want to write something more rigorous that treats stdout and stderr separately, you'll need to use proc_open() instead: PHP StdErr after Exec()
There are (perhaps insurmountable) difficulties when trying to execute sudo commands from a PHP script and from an external script called by PHP on SELinux enabled machines.
Make sure you use Linux directory path in your command
Linux won't let apache change the group id of the process by default.
You may need to use another solution, like make the PHP script deposit a file in a directory which is monitored by cron or inotify and which will call another script with root privileges.
Obviously it does not work on Linux. Command pushd is defined in windows shell only. The path on linux must use forward and not back slashe as separator.