简体   繁体   中英

Calling java program dependent on external library

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;)

没错,每个shell_exec都会创建一个单独的shell。

env CLASSPATH=whatever java -switches

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM