exec() on a java program in php-cgi on a raspberry pi - java

So, I have a php application, running through cgi, with nginx.
I have a .jar program used to print barcodes on, guess what, a barcode printer (Zebra)!.
My php application runs the .jar program using the exec() command. On my computer (php, cgi, nginx, debian testing), it works.
Now, I'm trying the same thing, but on a raspberry pi. Debian (wheezy, raspberry version), php, cgi, nginx.
The exec() command does not fail (in the way I dont get a huge fatal error), does not return anything, and does not succeed neither.
The java command works fine when I try it in the shell, using the www-data user (the one running nginx/php-cgi). It works when I put it in a php-cli (cli, not cgi) script, and run it with the php command in the shell, with the www-data user :
<?php exec('java -cp /path/ Methode options');?>
$ php print.php
Cups configuration seems ok (I (the pi user) can print, the www-data user can print through command line (shell), root can print, everyone can).
I'm confused and can't find where the problem is.

Try adding 2>&1 at the end of your exec(), in example: exec('java -cp /path/ Method options 2>&1');
Edit:
I would not advice you to do this if it was a real server; but since its a PI you can edit the sudoers file and add the following
run sudo visudo
and add:
www-data ALL=NOPASSWD: ALL
now modify your exec string with:
exec('sudo java -cp /path/ Method options');

Related

Invoking .jar file doesn't work in php shell_exec linux but it works in cmd and windows

I am working on a script that calls the Imagej java app to process an image uploaded by a PHP web page.
The problem occurs when the PHP shell_exec command calls the app.
The structure was tested in windows and worked correctly.
However, when trying to implement in linux, shell_exec halts the php script and stays loading forever. In the same shell_exec "java -version" worked, but calling the jar did not work.
the shell_exec command is:
java -Xmx512m -Dplugins.dir="/var/www/ImageJ/" -jar "/var/www/ImageJ/ij.jar" -batch "/var/www/ImageJ/macros/[macrofile]" [imagefile.jpeg]
However when I try to run in php from console, it runs.
php > shell_exec("java -Xmx512m -Dplugins.dir=\"/var/www/ImageJ/\" -jar \"/var/www/ImageJ/ij.jar\" -batch \"/var/www/ImageJ/macros/melaTest.ijm\" F15274739305711.jpeg");
Could it be a problem with php.ini or user?
The problem was fixed.
The issue was related to GUI nature of ImageJ, so the app expected a virtual screen to run from shell_exec.
To solve the problem it was installed Xvfb (sudo apt install xvfb) and the command inside php shell_exec was called from the command "xvfb-run" to execute in a virtual server.
xvfb-run -a -s "-screen 0 1024x768x24" java -Xmx512m -Dplugins.dir="/var/www/ImageJ/" -jar "/var/www/ImageJ/ij.jar" -batch "/var/www/ImageJ/macros/[macro...] [arg]

Php exec is not running properly when called from Java on localhost

I have a problem running the exec command from within a php script.
here's the detailed scenario:
I have an executable my_exe which runs from command line. This executable uses an environment variable, so I prepend the command with MY_VARIABLE='value' (works fine).
I use the exec command to run this executable from a Php script. ex: exec("MY_VARIABLE='value' my_exe"); (works fine too, tested in a browser)
The problem is in this part. When I call the php script from a Java program using HttpUrlConnection, I have two cases: if I run the Java program from outside the server (using public IP address) I get the good results, but when I run it from the server (using local IP, form command line ex. "java -jar /path/to/my/jar"), the Php works but the exec doesn't seem to be working properly. The environment variable is not being set in this case, so the executable runs but without the right values, so I get no results.
Any help is appreciated. I'm really stuck here. My guess is that there is a problem with Php or Java permissions, but I can't figure it out.
Strange command exec("MY_VARIABLE='value' my_exe");... it will work only on linux machine and will not on windows.
As a first step, try to rewrite you php code like this:
putenv("MY_VARIABLE=value");
exec("my_exe");
and see what happen.

Can't run java from PHP's exec

I've been trying to run a JAR file that would read from input.txt and write to output.txt this way in console:
java -jar file.jar input.txt output.txt
And it works 100% fine on my machine. I need to run it inside a php script, and this code works 100% fine for me (Mac OS, php built-in server):
exec("java -jar file.jar input.txt output.txt");
But once I deploy it (CentOS server) where the exec function is allowed, it fails, it returns an empty string and the jar does not work, running it directly from shell is OK.
How can I fix that?
Thanks in advance!
The problem with your exec() is that PHP doesn't know where Java is on the server. Update your command to specify the full path to the Java executable and it should work, though you should also use full paths to the jar and text files while you're at it.

Java JAR file does not execute in startup script in Ubuntu 14.04

The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.

running -jar file from php code

I want to run -jar file from my php script, my -jar file run perfectly through commandline but i want to integrate it to my website but it is not working properly and also not provide me any error. my code is below
if (isset($_POST['Export'])) {
exec('java -Xmx1024M -jar C:\UploadTest-1_2.jar http://localhost:8080/packaging/Package C:\book\testbook.pdf –pass park345');
echo 'export button clicked';
}
output is:
export button clicked but -jar not doing needful.
Is Java on the path?
Try the command with "java -version", if you don't get output, Java is not on the PATH defined within the PHP environment.
Is the environment locked down?
When running PHP code, one generally doesn't allow any kind of execution to take place, as people could attempt to hijack the PHP environment to place code on the server which wasn't there. This means that you will have to take into account any type of chroot'ing, acl controls, permissions, selinux, and web server configurations which make it harder to run items without a clear knowledge (that means prior configuration) that the application should be allowed to run.

Categories