473,581 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.d wf
/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 16956
On Fri, 06 Jan 2006 13:36:04 -0600, Good Man <he***@letsgo.c om> 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.u k :: 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.c om:
On Fri, 06 Jan 2006 13:36:04 -0600, Good Man <he***@letsgo.c om> 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.c om> wrote:
For example, the command that PHP gets is:
java -classpath /myDir:/myDir/CADViewer.jar CADViewer /myDir/
/myDir/conversions/3857/floor_1_chart.d wf
/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.headl ess=true

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be >
Jan 7 '06 #5
Good Man <he***@letsgo.c om> 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
8004
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 = CreateObject("ADODB.Connection") Cn.CursorLocation = adUseClient Cn.ConnectionTimeout = 0
6
5383
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 as SQL Server allows it as the exemple bellow shows : EXECUTE 'SELECT * FROM Test' Is there any way to do so with DB2 ?
14
11117
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 appreciated..
14
5889
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
3321
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 = wshell.run("%comspec% /c dir *.* > d:\project\test\text.txt", 0, True) Response.Write( intReturn ) set wshell = nothing
0
11694
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 java.sql.SQLException: Deadlock found when trying to get lock; Try restarting transaction message from server: "Lock wait timeout exceeded; try restarting...
6
6844
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 myjava" in my javascript. Thanks a million.
7
9577
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 image. But i executed that command in jsp Program .It didn't work.It shows error.
5
7049
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: <%@ page import="java.io.*" %> <HTML> <BODY> <% Runtime rt = Runtime.getRuntime(); Process p = rt.exec("/bin/ls");
0
7882
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5683
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.