By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,946 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,946 IT Pros & Developers. It's quick & easy.

Trying to execute Java command on server via PHP

P: n/a
Hi there

I am trying to execute a custom-built java program on my linux server via
PHP. Basically, a user uploads files via PHP, and then the java program
performs some action on these files.

I have successfully had other operations performed on these files by
using backticks, ie:
$doit = `my exec command`;

....because i have found that exec('my exec command'); never really works
properly.
To troubleshoot, I have had each 'command' echo'd to the screen. If I
copy and paste my command into the server's terminal window (via SSH),
everything works properly. However, nothing at all is happening when the
script is trying to issue the command.

For example, the command that PHP gets is:
java -classpath /myDir:/myDir/CADViewer.jar CADViewer /myDir/
/myDir/conversions/3857/floor_1_chart.dwf
/myDir/conversions/3857/floor1.txt

When that command is echo'd to my screen and I paste that into my
terminal window, the action is performed.

I don't think this is a server permissions issue as all files/JARs
involved are CHMOD'ed properly for execution/writing by the apache
'user'. Nor do I think this is a 'directory' restrictive thing, because I
can type that command regardless of the directory I am in and have it
work properly (as absolute paths are specified).

Does anyone have any tips? Why would the command be working from the
terminal window but not from the PHP script?

Thanks!!
Jan 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 06 Jan 2006 13:36:04 -0600, Good Man <he***@letsgo.com> wrote:
Why would the command be working from the
terminal window but not from the PHP script?


These things are almost always environment differences. Your terminal will be
running under your userid, and have different environment variables to the user
that Apache and hence PHP is running as.

If you run "env" (usually in /usr/bin or /bin) using exec from PHP then you
can get it to dump the environment that will be seen by processes run by exec -
I would bet that there are Java-related environment differences.

But before this, remember there are more arguments to exec:

http://uk.php.net/manual/en/function.exec.php

Check the return value. Check the output. You may also get some useful error
output if you redirect stderr back into stdout (e.g. "your-command 2>&1")

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jan 6 '06 #2

P: n/a
Andy Hassall <an**@andyh.co.uk> wrote in
news:9t********************************@4ax.com:
On Fri, 06 Jan 2006 13:36:04 -0600, Good Man <he***@letsgo.com> wrote:
Why would the command be working from the
terminal window but not from the PHP script?
If you run "env" (usually in /usr/bin or /bin) using exec from PHP
then you
can get it to dump the environment that will be seen by processes run
by exec - I would bet that there are Java-related environment
differences.


Hi Andy

Thanks for the tip. Indeed, comparing the "env" outputs, I see my telnet
user has "/usr/java/j2sdk1.4.2_06/bin:/usr/java/j2sdk1.4.2_06/jre/bin"
listed in the PATH: variable, while those lines are absent from the
apache user's PATH. I am guessing that the problem is exactly this -
the user 'apache' has no access to java.

I have spoken with the tech support people running my box and asked them
to add the Java path to the apache user, and I will post the result to
the NG for future reference and interest.

Jan 6 '06 #3

P: n/a
> Why would the command be working from the
terminal window but not from the PHP script?


Not sure, but my guess would be possibly due to php permissions. If you
can run it locally, you're able to perform commands on the system, sure,
but over the net, the account you are running apache under (or even in
the php.ini or httpd.conf) may not be allowed access to execute
commands. Maybe you've already considered this, but I would look into
the permissions...

-John D. Mann
Jan 6 '06 #4

P: n/a
On 2006-01-06, Good Man <he***@letsgo.com> wrote:
For example, the command that PHP gets is:
java -classpath /myDir:/myDir/CADViewer.jar CADViewer /myDir/
/myDir/conversions/3857/floor_1_chart.dwf
/myDir/conversions/3857/floor1.txt
Have you tried with /full/path/to/java ... ?
I don't think this is a server permissions issue as all files/JARs
involved are CHMOD'ed properly for execution/writing by the apache
'user'. Nor do I think this is a 'directory' restrictive thing, because I
can type that command regardless of the directory I am in and have it
work properly (as absolute paths are specified).


Are you doing some image transformations? Or something else that uses java.awt
stuff? Very often a webserver doesn't have a X installation, and this will make
java choke. The solution is to start java with the option
-Djava.awt.headless=true

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
Jan 7 '06 #5

P: n/a
Good Man <he***@letsgo.com> wrote in
news:Xn***********************@216.196.97.131:
Thanks for the tip. Indeed, comparing the "env" outputs, I see my
telnet user has
"/usr/java/j2sdk1.4.2_06/bin:/usr/java/j2sdk1.4.2_06/jre/bin" listed
in the PATH: variable, while those lines are absent from the apache
user's PATH. I am guessing that the problem is exactly this - the
user 'apache' has no access to java.

I have spoken with the tech support people running my box and asked
them to add the Java path to the apache user, and I will post the
result to the NG for future reference and interest.


To follow up, this was exactly the problem. Adding the path to the apache
user enabled me to call a java program via PHP.

Hope this helps someone in the future too!
Jan 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.