Help us understand the problem. What is going on with this article?

MeCab:Javaバインディング開発環境構築、実装参考ページ等

More than 5 years have passed since last update.

MeCabとは、オープンソースの形態素解析エンジン。
MeCab Yet Another Part-of-Speech and Morphological Analyze[http://mecab.googlecode.com/svn/trunk/mecab/doc/index.html]

MeCabのJavaバインディングを構築した環境

centos-6.4(vagrant+virtual-box)
MeCab-0.996
MeCab-ipadic-2.7.0-20070801
openjdk1.7
openjdk1.7-devel

centos-6.4(vagrant+virtual-box)の構築

VirtualBox:Vagrant:仮想環境を構築する
Lunux:CentOS:日本語化に対応する

javaのインストール

$ yum search openjdk
$ su
# yum install java-1.7.0-openjdk
# yum install java-1.7.0-openjdk-devel

環境変数の設定

export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8

設定せずにのちの作業を実行するとmecab-javaをmakeしたときに下記のエラーが出る

'Unmappable character for encoding ASCII but my files are in UTF-8'

mecabとその辞書をインストールする

mecabのインストール

$ sudo yum install wget
$ wget --no-check-certificate 'https://docs.google.com/uc?export=download&id=FILEID' -O FILENAME
# URL'https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7cENtOXlicTFaRUE
# FILENAME= mecab-0.996.tar.gz
$ tar zxfv mecab-0.996.tar.gz
$ cd mecab-0.996
$ ./configure  --with-charset=utf8 //文字コード
$ make
$ make check
$ su
# make install
# mecabのライブラリ
$ ls /usr/local/lib
libmecab.a  libmecab.la  libmecab.so  libmecab.so.2  libmecab.so.2.0.0

mecab-ipadicのダウンロード

$ wget --no-check-certificate 'https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7MWVlSDBCSXZMTXM' -O mecab-ipadic-2.7.0-20070801.tar.gz
$ tar zxfv mecab-ipadic-2.7.0-20070801.tar.gz
$ mv mecab-ipadic-2.7.0-20070801 mecab-ipadic
$ cd mecab-ipadic
$ ./configure  --with-charset=utf8
$ make
$ su
# make install

mecabのjavaバインディング

JNI(Java Native Interface):JavaからC言語/C++の関数を呼ぶ・あるいは逆にC/C++からJavaのメソッドを呼ぶための仕組み。

mecab-javaのダウンロード

$ wget http://pkgs.fedoraproject.org/repo/pkgs/mecab-java/mecab-java-0.996.tar.gz/e50066ae2458a47b5fdc7e119ccd9fdd/mecab-java-0.996.tar.gz
$ tar vzxf mecab-java-0.996.tar.gz    // 展開
$ mv mecab-java-0.996 mecab-java      // 名前変更
$ cd mecab-java

Makefileの設定を修正する

$ vi Makefile
INCLUDE=/usr/lib/jvm/java-6-openjdk/include
~
~
LIBS=`mecab-config --libs`
INC=`mecab-config --cflags` -I$(INCLUDE) -I$(INCLUDE)/linux
INCLUDE=/usr/lib/jvm/java-1.7.0-openjdk.x86_64/include
~
~
LIBS=-arch x86_64 `mecab-config –libs`
INC=-arch x86_64 `mecab-config –cflags`
$ ls -l /usr/lib/jvm
total 4
lrwxrwxrwx 1 root root   26 May 19 02:47 java -> /etc/alternatives/java_sdk
lrwxrwxrwx 1 root root   32 May 19 02:47 java-1.7.0 -> /etc/alternatives/java_sdk_1.7.0
drwxr-xr-x 7 root root 4096 May 19 02:47 java-1.7.0-openjdk-1.7.0.79.x86_64
lrwxrwxrwx 1 root root   34 May 19 02:47 java-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.79.x86_64
lrwxrwxrwx 1 root root   34 May 19 02:47 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx 1 root root   21 May 19 02:46 jre -> /etc/alternatives/jre
lrwxrwxrwx 1 root root   27 May 19 02:46 jre-1.7.0 -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx 1 root root   38 May 19 02:46 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.79.x86_64/jre
lrwxrwxrwx 1 root root   29 May 19 02:46 jre-openjdk -> /etc/alternatives/jre_openjdk

mecab-javaのインストール(MeCab.jarとlibMeCab.soが生成される)

$ su
# make
# ls
AUTHORS  BSD  COPYING  GPL  LGPL  Makefile  MeCab.jar  MeCab_wrap.cxx  MeCab_wrap.o  README  bindings.html  libMeCab.so  org  test.class  test.java

test.javaを実行すると、java.library.pathにMeCabが配置されていないためエラーが出る。

環境変数LD_LIBRARY_PATH

$ java test
Cannot load the example native code.
Make sure your LD_LIBRARY_PATH contains '.'
java.lang.UnsatisfiedLinkError: no MeCab in java.library.path
$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
$ vi .bash_profile  // 以下の情報を末尾に記載
.bash_profile
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

etcとldconfig

$ javac -cp MeCab.jar test.java
$ java -cp MeCab.jar:. test

java.library.pathを調べて、libMeCab.soファイルを移動させる

CheckPropクラスの作成

CheckProp.java
public class CheckProp {
  public static void main(String[] args) {
     System.out.println(System.getProperty(args[0]));
  }
}

コンパイルして実行する。引数にjava.library.pathを指定する。

$ javac CheckProp.java
$ java CheckProp java.library.path
/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした