running a jar file in PHP - php

I have a command to be run like this
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
I have echoed the $command variable and I get the output as
java -jar X:\wamp\www\moodle\gmksplit.jar X:\wamp\www\moodle/upload/maze_4.gmk X:\wamp\www\moodle/outputs/maze_4;
which is exactly I want to run..
I am trying to run it as
echo $exec($command);
it is not running. I have tried all the functions like shell_exec() and system()
It gives the output as
Java Version: 10700 (1.7.0_01)
when i run the same line in command prompt I get the output as
Java Version: 10700 (1.7.0_01)
Loading lib files in X:\wamp\www\moodle\gmksplit.jar
01_move.lgl 02_main1.lgl 03_main2.lgl 04_control.lgl
05_score.lgl 06_extra.lgl 07_draw.lgl
time taken to load file: 254 ms
so, as you see my php code is giving only the first line as output. The command is not running properly and I am not getting the intended output.
please help me
I am using the wampp server

Can you try this:
<?php
$command="java -jar ".dirname(__FILE__)."\gmksplit.jar"." ".$input_path." ".$output_path;
$out = array();
exec = ($command, $out);
print_r($out);
?>

Related

Running exec command php

I have just installed pdf2htmlEX without any issues. If I run the command on the server it works fine. So I know the library itself is doing what it is supposed to do. If I run the command in php via the exec function nothing happens. I assume exec is used in this instance??
In PHP
// doesn't work
$output = exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf');
// doesn't work
$output = shell_exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf');
If I run a general command in the exec function it works fine, so I know exec is enabled and working as expected.
// works fine
$exec = exec('pwd', $output);
print_r($output);
Direct on Command Line not in PHP
// works and generates the file as expected.
pdf2htmlEX --zoom 1.3 pdf/test.pdf
Any help would be greatly appreciated.
Command line reference for library
https://github.com/coolwanglu/pdf2htmlEX/wiki/Quick-Start
EDIT:
After some further digging, it could be that the php script runs as a different user to the command line. My question would then be how do I check/fix this?
You just have to add the output html path and file name in your command :
Most of pdf_to_something packages usage with exec() is package_name [options] <input-filename> <output-filename>
$path = 'path/to/your/folder/';
$command = 'pdf2htmlEX '.$path.'test.pdf '.$path.'test.html';
exec($command);
Try following and check if your file is created in working directory.
exec('pdf2htmlEX --zoom 1.3 pdf/test.pdf',$output);
pdf2htmlEX doesn't output anything.
$cmd= "pdf2htmlEX --zoom 1.3 test/test.pdf";
exec($cmd, $output);
Will create a test.html file in the current directory.
Are you expecting something else?
Note: i've tested this on ubuntu by installing pdf2htmlEX with
sudo apt-get install pdf2htmlex
To get the output,
$a = file_get_contents("test.html");
echo $a;

Running PhantomJS from PHP with exec()

I've got the following script:
#!/bin/sh
export DISPLAY=:0
phantomjs --version
It try to run it from the following PHP script:
<?php
$result = shell_exec('sh test.sh');
echo $result;
?>
This script return the following error:
[Thu Jun 19 10:31:31 2014] [error] [client] test.sh: line 3: phantomjs: command not found
I tried to run phantomjs -v by hand in a console, and it runs fine. I checked the PATH, and phantomjs is correctly defined and found.
The execution environment is a virtual Server with LiveConfig.
Can someone help me understand what I'm doing wrong ?
It could be an issue with shell_exec() and line breaks,
try adding "2>&1" to the string you are passing:
$result = shell_exec('sh test.sh 2>&1');
this worked for me, found it in the top comment here, naturally ;)
Your PATH probably lacks the location for the phantomjs executable. PhantomJS is probably installed in /usr/local/bin so you need to add this to your PATH variable:
#!/bin/sh
export DISPLAY=:0
PATH=$PATH:/usr/local/bin
phantomjs --version
To check what the current PATH is, you could begin the shell script with:
#!/bin/sh
echo $PATH
<?php
exec('/usr/local/bin/phantomjs path/somescript.js');
?>
Yes. Sometimes phantomjs don't need full path in some environment without generate any error. However, sometimes it does.
Always use the full path for all argument in the php command.
Did you use the fullpath for hello.js?
Do not use exec(). Never. It's a bad way.
Use the php-phantomjs and PhantomJS Runner instead.

Execute multiple sage commands from php script

I have installed Sage math on my ubuntu server. I can run sage commands by ssh terminal such as
$sage:
$sage: f = x^2
$sage: f.diff(x)
I would like to do on a php script
exec('sage');
exec('sage: f = 5x^3');
$fprime = exec('sage: latex(f.diff(x))');
echo $fprime;
I would expect "15x^2" as output but that's not happening .. however on ssh terminal all is good..
all help would be greatly appreciated..
I'm not completely sure of what you want to achieve. PHP's exec function executes a shell command, and that's all. Read http://fr2.php.net/manual/en/function.exec.php.
sage: f = 5*x^3 is not a shell command. You can run sage scripts directly via the command line using the -c switch :
./sage -c 'print latex((5*x^3).diff())'
will print 15 \, x^{2} to the terminal. In PHP you can grab this output by passing a second argument to exec:
exec("./sage -c 'print latex((5*x^3).diff())'", $output);
echo $output[0];

How to compile a c code on windows command prompt by using php

Using passthru function in PHP I can run an exe e.g hello.exe and get its result but unable to compile e.g hello.c using command tcc hello.c on windows command prompt through php script.
So question is how to run commad tcc hello.c on windows command prompt using php script. php script & cmd.exe are on the came machine.
OS windows XP.
code that i want to compile is hello.c is given below.
php script that is executing hello.exe is also given below.
code from hello.c
#include<stdio.h>
void main(void)
{
printf("hello");
}
php code that is succefully accesing and displaying hello.exe
enter code here
<?php
echo "Remote compilation:";
$path = escapeshellcmd('C:\\TC\\BIN\\HELLO.exe');
passthru( $path, $return_var);
?>
please guide
regards
From the PHP documentation:
The passthru() function is similar to the exec() function in that it
executes a command. This function should be used in place of exec() or
system() when the output from the Unix command is binary data which
needs to be passed directly back to the browser.
But here you are not expecting binary data to be generated from tcc and passed to the browser. A more appropriate function to use would be shell_exec(). E.g.:
<?php
echo "Remote compilation:";
$command = escapeshellcmd('YOUR TCC COMPILE COMMAND HERE');
shell_exec($command);
?>

PHP and Apache : shell_exec(wkhtmltopdf with xvfb) command doesn't work

I try to run the wkhtmltopdf (0.11.0 rc1) with php (5.4.2) on apache (2.4.2).
When I try to launch wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1, I can find my pdf. Here my php code
<?php
$cmd= '/usr/bin/wkhtmltopdf-i386 http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
It works with apache and as command line php test.php.
Because my target page contains many images and some "heavy" js charts. I have got a Segmentation Fault with the wkhtmltopdf command when I try to turn it into pdf.
The only way to make it work is to use xvfb as X11 emulator. The code looks like this :
<?php
$cmd= '/usr/bin/xvfb-run /usr/bin/wkhtmltopdf-i386 --use-xserver http://www.google.com google.pdf 2>&1';
$ret = shell_exec($cmd);
echo $ret;
?>
This script works with the command line php test.php but it doesn't work with apache. If I take a look into the apache's process with htop, I can see that there are two process (with php test.php) :
xvfb
wkhtmltopdf
When I launch with apache I have only xvfb process. It finish by a timeout from apache because it's waiting the wkhtmltopdf process.
I can make it works with apache (2.2.21) and php (5.3.10).
Is there something that I'm missing ? Maybe something in the apache's config-files ?
I was having the same problem. I was using the exec function, but the same applies to shell_exec. The function execution was disabled in php.ini.
SOLUTION: Remove the shell_exec string from the disable_functions at php.ini file.
I am not sure why your second version is not callable from Apache (must not be using the same shell, since shell_exec uses a shell?), but as a work-around could you (from Apache PHP) shell_exec("php test.php"); and get your intended result?
Perhaps also try one of the other process execution functions such as pcntl_exec.
it's mostly because of ownership and permissions, try
su www-data (for debian)
php test.php
you'll probably see the error.

Categories