(1) Javaの設置
jarファイルで設置も考えましたが、変更もあり得るのでそのまま設置しました。
今回は以下を設置しました。
https://github.com/smzn/Sentiment
今回は外部jarファイルがあるので、/usr/local/lib にjarファイルを入れておき
$ vi ~/.bashrc
export CLASSPATH=/usr/local/lib/mysql-connector-java-5.1.45-bin.jar:/usr/local/lib/io.indico-3.15.0.jar:/usr/local/lib/io.indico-3.15.0-jar-with-dependencies.jar:$CLASSPATH
と設定しました。
全ユーザに対して設定する場合は /etc/profileに書きます。
実行できることを確認します。
(2)シェルスクリプトの作成
シェルスクリプトを作成し、置いておきます。クラスパスは必要です。
$ vi ~/indico/sentiment.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
export CLASSPATH=/usr/local/lib/mysql-connector-java-5.1.45-bin.jar:/usr/local/lib/io.indico-3.15.0.jar:/usr/local/lib/io.indico-3.15.0-jar-with-dependencies.jar:$CLASSPATH | |
cd /home/ms000/indico | |
javac sentiment/*.java | |
java sentiment.Sentiment_main |
$ chmod +x ~/indico/sentiment.sh
シェルスクリプトから実行できることを確認します。
$ ~/indico/sentiment.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/bin/sh export CLASSPATH=/usr/local/lib/mysql-connector-java-5.1.45-bin.jar:/usr/local/lib/io.indico-3.15.0.jar:/usr/local/lib/io.indico-3.15.0-jar-with-dependencies.jar:$CLASSPATH cd /home/ms000/indico javac sentiment/*.java java sentiment.Sentiment_main
(3) PHPから呼び出しThis file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/bin/sh export CLASSPATH=/usr/local/lib/mysql-connector-java-5.1.45-bin.jar:/usr/local/lib/io.indico-3.15.0.jar:/usr/local/lib/io.indico-3.15.0-jar-with-dependencies.jar:$CLASSPATH cd /home/ms000/indico javac sentiment/*.java java sentiment.Sentiment_main
PHPから呼び出すときに、exec関数を利用します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function sentiment($id = null) { | |
if (!$this->Keyword->exists($id)) { | |
throw new NotFoundException(__('Invalid keyword')); | |
} | |
$options = array('conditions' => array('Keyword.' . $this->Keyword->primaryKey => $id)); | |
$cmd = 'sh /home/ms000/indico/sentiment.sh'; | |
exec($cmd, $opt); | |
//print_r($opt); | |
$this->set('keyword', $this->Keyword->find('first', $options)); | |
} |
これでPHPからJavaを実行できました。