由于thrift支持多语言,所以编译thrift源码的过程中,会用到该语言的一些类库,如c++的boost、java的jdk等。
那么,在安装thrift过程中,需要对各种语言安装哪些类库和工具呢,官方对此有详细的介绍:
所需语言库和工具 C++ :Boost 1.33.1+ (必选),libevent (可选,用来创建非阻塞服务器) ,zlib (可选) Java :Java 1.5+ (必选),Apache Ant (必选),Apache Ivy(必选),Apache Commons Lang,SLF4J(必选) C#: Mono 1.2.4+ 或者 Visual Studio 2005+ Python: 2.4+(必选) PHP: 5.0+(必选) Ruby: 1.8+(必选) Erlang: R12(必选) Perl 5: Bit::Vector ,Class::Accessor 以上的语言库是看你具体需要thrift生成怎样的语言,并不是必须全部都安装。
thrift支持windows和linux两种操作系统
未尝试windows下的安装(考虑到普遍性和高效率,不建议使用windows安装thrift),以下是网上搜到windows下安装方法:
http://wiki.apache.org/thrift/ThriftInstallationWin32
http://blog.csdn.net/snowbirdfly/article/details/6746392
linux下安装,本人尝试过centos4和centos5,由于centos5自带的一些开发包版本较高,和thrift环境比较吻合,建议使用centos5.
下面开始在centos5下安装thrift,let’s begin 。
centos5自带的jdk版本是1.6.0,符合thrift安装要求,可能需要添加环境变量。
环境变量添加方法: vim /etc/profile 此文件后面追加(路径因机而异): JAVA_HOME=/usr/share/jdk1.5.0_16 CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH LD_LIBRARY_PATH=/usr/lib export PATH CLASSPATH JAVA_HOME LD_LIBRARY_PATH 执行以下命令,使profile文件生效: source /etc/profile
验证方法:
输入java -version看java版本,须在1.5以上 输入javac,瞧见此命令的使用说明后,即验证jdk安装成功,否则,检查一下就哪个环节出了问题。
这个安装比较简单,不懂的地方可以参考: http://www.blogjava.net/esprit/archive/2006/01/18/28489.html
从 http://ant.apache.org/bindownload.cgi 下载最新的tar包:apache-ant-1.8.3.tar.gz。
cd到/usr/local下,解压
[root@bogon san1156]# tar zxpvf apache-ant-1.8.3.tar.gz [root@bogon san1156]# ln -s apache-ant-1.8.3 ant
将ANT_HOME设置到当前用户的.bash_profile文件
vi /root/.bash_profile export ANT_HOME=/usr/local/ant export PATH=/usr/local/ant/bin:$PATH
用ant命令测试运行情况
[root@bogon san1156]# ant Buildfile: build.xml does not exist! Build failed [root@bogon san1156]# ant -version Apache Ant(TM) version 1.8.3 compiled on February 26 2012
若出现以上内容,说明正常安装成功
否则,若出现这样的错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/Launcher
这是linux系统默认安装了一些ant的lib,修改 /etc/ant.conf中 ANT_HOME=/usr/share/ant 为你正确安装ant的地址,如 /usr/local/ant
注:编译thrift工程要用到ant,thrift中的java工程配置好了build.xml,故不需要手动修改此文件。
参考网址:http://blog.csdn.net/nivana999/article/details/6526961
下载apache ivy:http://labs.mop.com/apache-mirror//ant/ivy/2.2.0/apache-ivy-2.2.0-bin.tar.gz
tar -xzvf apache-ivy-2.2.0-bin.tar.gz
cp ivy-2.2.0.jar ANT_HOME/lib
cd 到 apache-ivy-2.2.0/src/example/hello-ivy,运行ant
如果看到:
BUILD SUCCESSFUL Total time: 29 seconds
就代表欧了
智能安装命令,此命令需要远程下载一些安装包,并自动安装。下载文件大小共计60M,时间根据你的下载速度而定。
sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
官方说明: Installing the required packages on CentOS 5 http://wiki.apache.org/thrift/GettingCentOS5Packages
http://mirror.bjtu.edu.cn/apache/thrift/0.8.0/thrift-0.8.0.tar.gz
tar -zxvf thrift-0.8.0.tar.gz
cd thrift-0.8.0 ./configure make make install
在命今行内输入thrift,如果有提示命令使用方法,证明可以了。
如果提示命令不可用,请接着本文向下看,对症下药,方可药到病除。
事无巨细,再简单的事情,做起来往往也不是一帆风顺,更何况是部署强大的thrift系统了。 不过,经历过风雨,才能见到最灿烂的彩虹!只有经受了层层不绝的bug磨练,才能锻炼出程序员这颗坚强勇敢的心!
yum install openssl openssl-devel libmcrypt libmcrypt-devel curl-devel
此命令自动下载安装程序,并安装,可谓快捷方便,纯傻瓜式操作
Building code generators ..... : Building C++ Library ......... : yes Building C (GLib) Library .... : no Building Java Library ........ : no Building C# Library .......... : no Building Python Library ...... : yes Building Ruby Library ........ : no Building Haskell Library ..... : no Building Perl Library ........ : no Building PHP Library ......... : yes Building Erlang Library ...... : no Building Go Library .......... : no Building TZlibTransport ...... : yes Building TNonblockingServer .. : yes
其实,原因也无非是安装jdk、ant或者ivy不成功导致。本人安装是碰到的问题是:
configure:16550: checking for javac and java Running "javac configtest_ax_javac_and_java.java" Running "java configtest_ax_javac_and_java" Exception in thread "main" java.lang.NoClassDefFoundError: configtest_ax_javac_and_java Running "kaffe configtest_ax_javac_and_java" ./configure: line 16569: kaffe: command not found Running "jikes configtest_ax_javac_and_java.java" ./configure: line 16562: jikes: command not found Running "gcj -C configtest_ax_javac_and_java.java" Running "java configtest_ax_javac_and_java" Exception in thread "main" java.lang.NoClassDefFoundError: configtest_ax_javac_and_java Running "kaffe configtest_ax_javac_and_java" ./configure: line 16569: kaffe: command not found configure:16583: result: no
此问题的解决,不要去找configtest_ax_javac_and_java.java这个是否存在,因为找半天也找不到。
这种错误往往是环境变量classpath配置有问题,需要在classpath前面添加“./:”,默认从同级目录下找java工程文件。
Making all in java make[3]: Entering directory `/home/san1156/soft/thrift-0.8.0/lib/java‘ /usr/local/ant/bin/ant Buildfile: /home/san1156/soft/thrift-0.8.0/lib/java/build.xml setup.init: mvn.ant.tasks.check: proxy: mvn.ant.tasks.download: [get] Getting: http://repo1.maven.org/maven2/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar [get] To: /home/san1156/soft/thrift-0.8.0/lib/java/build/tools/maven-ant-tasks-2.1.3.jar [get] Not modified - so not downloaded mvn.init: Unable to obtain resource from /home/san1156/soft/thrift-0.8.0/lib/java/build/tools/maven-ant-tasks-2.1.3.jar: java.util.zip.ZipException: error in opening zip file [typedef] Unable to obtain resource from /home/san1156/soft/thrift-0.8.0/lib/java/build/tools/maven-ant-tasks-2.1.3.jar: [typedef] java.util.zip.ZipException: error in opening zip file [typedef] at java.util.zip.ZipFile.open(Native Method) [typedef] at java.util.zip.ZipFile.<init>(ZipFile.java:127) [typedef] at java.util.jar.JarFile.<init>(JarFile.java:135) [typedef] at java.util.jar.JarFile.<init>(JarFile.java:99) [typedef] at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLoader.java:1006) [typedef] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.findNextResource(AntClassLoader.java:149) [typedef] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.<init>(AntClassLoader.java:110) [typedef] at org.apache.tools.ant.AntClassLoader.findResources(AntClassLoader.java:953) [typedef] at org.apache.tools.ant.AntClassLoader.getNamedResources(AntClassLoader.java:922) [typedef] at org.apache.tools.ant.loader.AntClassLoader5.getResources(AntClassLoader5.java:56) [typedef] at org.apache.tools.ant.taskdefs.Definer.resourceToURLs(Definer.java:375) [typedef] at org.apache.tools.ant.taskdefs.Definer.execute(Definer.java:267) [typedef] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291) [typedef] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) [typedef] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [typedef] at java.lang.reflect.Method.invoke(Method.java:597) [typedef] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) [typedef] at org.apache.tools.ant.Task.perform(Task.java:348) [typedef] at org.apache.tools.ant.Target.execute(Target.java:392) [typedef] at org.apache.tools.ant.Target.performTasks(Target.java:413) [typedef] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399) [typedef] at org.apache.tools.ant.Project.executeTarget(Project.java:1368) [typedef] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) [typedef] at org.apache.tools.ant.Project.executeTargets(Project.java:1251) [typedef] at org.apache.tools.ant.Main.runBuild(Main.java:811) [typedef] at org.apache.tools.ant.Main.startAnt(Main.java:217) [typedef] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280) [typedef] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109) [typedef] Could not load definitions from resource org/apache/maven/artifact/ant/antlib.xml. It could not be found. BUILD FAILED /home/san1156/soft/thrift-0.8.0/lib/java/build.xml:267: Problem: failed to create task or type antlib:org.apache.maven.artifact.ant:remoteRepository Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any <presetdef>/<macrodef> declarations have taken place. No types or tasks have been defined in this namespace yet This appears to be an antlib declaration. Action: Check that the implementing library exists in one of: -/usr/local/ant/lib -/root/.ant/lib -a directory added on the command line with the -lib argument Total time: 0 seconds make[3]: *** [all-local] Error 1 make[3]: Leaving directory `/home/san1156/soft/thrift-0.8.0/lib/java‘ make[2]: *** [all-recursive] Error 1 make[2]: Leaving directory `/home/san1156/soft/thrift-0.8.0/lib‘ make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory `/home/san1156/soft/thrift-0.8.0‘ make: *** [all] Error 2
2、处理方法: It could be related to log4j. http://stackoverflow.com/questions/325202/java-util-zip-zipexception-error-in-opening-zip-file
另外,thrift官网要求java必须安装SLF4J,故,基本确定是未安装SLF4J导致。
官网上下载apache-log4j-1.2.16.tar.gz、slf4j-1.6.4.tar.gz
下载地址分别是: http://mirror.bjtu.edu.cn/apache/logging/log4j/1.2.16/apache-log4j-1.2.16.tar.gz
http://www.slf4j.org/dist/slf4j-1.6.4.tar.gz
将需要的jar包放到执行位置,操作如下:
需要的jar包:log4j-1.2.16.jar、slf4j-api-1.6.4.jar、slf4j-log4j12-1.6.4.jar
指定位置:/home/san1156/soft/jdk1.6.0_30/lib/,也就是jdk的classpath路径。
由于报错的时候产生了一些临时文件,需要执行
[root@localhost thrift-0.8.0]# make clean
或者
[root@localhost java]# cd /home/san1156/soft/thrift-0.8.0/lib/java [root@localhost java]# ant clean
[root@localhost thrift-0.8.0]# make
或者
[root@localhost java]# cd /home/san1156/soft/thrift-0.8.0/lib/java [root@localhost java]# ant 执行通过,说明java环境木有问题
以上问题和解决方法,是本人编译过程中所总结,希望对后来者有用 如果不幸,你碰到的问题,这里没有述及,可以联系本人一块探讨
http://blog.163.com/solylee@126/blog/static/171823157201101811587203/
http://hi.baidu.com/lbxthinker/blog/item/527b88ca1d8002f253664f68.html
(centos应用程序有自己的boost,点击更新即可完成安装,就是版本较低)
http://gcc.gnu.org/mirrors.html
http://zhidao.baidu.com/question/1251827.html
http://apps.hi.baidu.com/share/detail/3320416
http://blog.csdn.net/nivana999/article/details/6526961
export ANT_HOME=/home/san1156/soft/apache-ant-1.8.3 export PATH=/home/san1156/soft/apache-ant-1.8.3/bin:$PATH 修改/root/.bash_profile这个文件,后面添加上面的环境变量 校验ant方法:直接输入ant,提示需要build.xml文件
http://www.cnblogs.com/ewyb/archive/2010/10/26/1861744.html
官方文档:
编译安装thrift
http://wiki.apache.org/thrift/ThriftInstallation
Installing the required packages on CentOS 5
http://wiki.apache.org/thrift/GettingCentOS5Packages
原文地址:http://blog.csdn.net/san1156/article/details/41146483