473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Trying to execute Java command on server via PHP

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
5 16946
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Me | last post by:
hi, I have this file I need to execute from an ASP client. since we have installed it on the SQL Database server machine, I thought I could run it like this: Set Cn =...
6
by: Doohan W. | last post by:
Hi, I'm now working with DB2, and I can't find out how to execute the contents of a string Statement, without using a Java/... procedure, only using SQL statements. I know that some SQBDs such...
14
by: technocrat | last post by:
db2 -t -v -f/home.../filename >output_file-name I have a java stored procedure..which has to run the above command...not sure how i can run this command through java.. any suggestions are...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
0
by: dappyl | last post by:
Hi,... I have an example shell script to run command prompt. It works fine.. the code shown below : <% Dim wshell, intReturn set wshell = server.createobject("wscript.shell") intReturn...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
6
by: moongeegee | last post by:
I have compile my java program as myjava.class. And I can run as "java myjava" without any program. As now, I need to execute myjava.class in javascript. Please shed a light how to execut "java...
7
by: swethak | last post by:
Hi, i have a command to convert the video file into image ffmpeg -i sample.wmv -f image2 -t 0.001 -ss 3 ss.jpg i run that one in command prompt it converted the video file into...
5
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.