views:

221

answers:

4

I am trying to call a java program in php to use it with web interface.

Java program is dependent on an external lib: commons-cli-1.2.jar

So basically I need to export it before calling the java program; but if I export it first as:

shell_exec('export CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar');

then call the java program as:

shell_exec('java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o');

I think it creates different shells for each call; then the export does not have any effect on java program. Or am I wrong?

Otherwise, it should output a file in the server. But simply it does not. So, what is wrong? Any idea?

edit: However can it be because some parameters such as para_i stands for an input file name, so that i have to specify full path for that? Because I just assume if the input file is in the same working directory, there won't be any problem, will it?

edit-2: it outputs properly when i use command line;)

A: 

you're right, each shell_exec creates a separate shell.

env CLASSPATH=whatever java -switches
just somebody
A: 

You should be able to call it like this.

shell_exec('java -cp $CLASSPATH:~/lib/commons-cli-1.2.jar ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');

Another option is to issue the 2 commands seperately, but to the same shell, like this:

shell_exec('export CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar; java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');

edit: some shells don't let you call export while you're setting up the variable. so this may be safer than the second option above:

shell_exec('CLASSPATH=$CLASSPATH:~/lib/commons-cli-1.2.jar; export CLASSPATH; java ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');

another edit: If none of the above work then you're going to have to do some more trouble shooting. Does your java program work from the command prompt?

java -cp $CLASSPATH:/home/user/lib/commons-cli-1.2.jar ComputePagerank -i param1 -d param2 -e param3 -o param4 > message
Glen
well, i tried the second method before, also. but still did not work. i also do the parameter checking, but simply they are also properly passed. i don't know what is going behind shell_exec.
israkir
third method also did not. btw can it be because some parameters such as para_i stands for an input file name, so that i have to specify full path for that? basically i assume if it is in the same working directory, there won't be problem.
israkir
A: 

I would use

shell_exec('java -cp $CLASSPATH:/home/yourname/dir/lib/commons-cli-1.2.jar ComputePagerank -i $para_i -d $para_d -e $para_e -o $para_o > message');

and (this is important) replace the tilde(~) with the actual path to your directory (/home/yourname say). The ~ is expanded by the shell and is dependent on which shell you''re using.

Brian Agnew
well, i also tried it with fullpath. still did not work.
israkir
A: 

Try Creating a simple shell script with the commands that you want to execute. You may pass arguments to a shell script so that is not a problem either.

for example

 echo "Running Script..."
 java -cp $CLASSPATH:~/lib/commons-cli-1.2.jar ComputePagerank -i $1 -d $2 -e $3 -o $4 > message

etc.

Then try calling it from the command line first with some parameters. Did it output? Then try calling it from the php script. Did it output? If it did not then you may need to check permissions. I had a simiolar experience some time ago with a Java program that simply did not have permission to write a file.

Vincent Ramdhanie
yes, it outputs properly when i use command line;)
israkir
Let your php script call the shell script that you created.
Vincent Ramdhanie