简体   繁体   English

依赖外部库调用Java程序

[英]Calling java program dependent on external library

I am trying to call a java program in php to use it with web interface. 我正在尝试在php中调用Java程序以将其与网络界面一起使用。

Java program is dependent on an external lib: commons-cli-1.2.jar Java程序依赖于外部库:commons-cli-1.2.jar

So basically I need to export it before calling the java program; 因此,基本上我需要在调用Java程序之前将其导出; but if I export it first as: 但如果我先将其导出为:

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

then call the java program as: 然后将Java程序调用为:

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. 那么导出对Java程序没有任何影响。 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? 编辑:但是这可能是因为诸如para_i之类的某些参数代表输入文件名,所以我必须为此指定完整路径吗? 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;) 编辑2:当我使用命令行时,它会正确输出;)

没错,每个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: 另一个选择是分别发出2条命令,但要在同一shell上,如下所示:

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. 编辑:在设置变量时,某些shell不允许您调用export。 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程序可以在命令提示符下工作吗?

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). 并且(这很重要)将波浪号(〜)替换为目录的实际路径( /home/yourname )。 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. 尝试使用要执行的命令创建一个简单的Shell脚本。 You may pass arguments to a shell script so that is not a problem either. 您可以将参数传递给Shell脚本,因此也不是问题。

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. 然后尝试从php脚本调用它。 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. 前段时间我对Java程序有类似的体验,而Java程序根本没有写文件的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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