开发者

Calling java program dependent on external library

开发者 https://www.devze.com 2022-12-13 05:05 出处:网络
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

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? Bec开发者_如何学Cause 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;)


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

env CLASSPATH=whatever java -switches


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消