1. 程式人生 > 實用技巧 >解決啟動時報ClassNotFoundException: org.apache.http.client.HttpClient異常的問題

解決啟動時報ClassNotFoundException: org.apache.http.client.HttpClient異常的問題

最近在研究阿里的分散式事務框架GTS,在執行sample-txc-simple時,拋以下異常:

JM.Log:INFO Set diamond-client log path: C:\Users\chanson\logs\diamond-client
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
    at com.taobao.txc.common.config.ConsoleConfig.g(Unknown Source)
    at com.taobao.txc.common.config.ConsoleConfig.
<init>(Unknown Source) at com.taobao.txc.common.config.ConsoleConfig.a(Unknown Source) at com.taobao.txc.common.config.o.a(Unknown Source) at com.taobao.txc.common.config.o.a(Unknown Source) at com.taobao.txc.common.LoggerWrap.a(Unknown Source) at com.taobao.txc.common.config.o.a(Unknown Source) at com.taobao.txc.client.a.a(Unknown Source) at com.taobao.txc.client.a.
<init>(Unknown Source) at com.taobao.txc.client.a.a(Unknown Source) at com.taobao.txc.client.aop.TxcTransactionScaner.a(Unknown Source) at com.taobao.txc.client.aop.TxcTransactionScaner.onApplicationEvent(Unknown Source) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:
96) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:954) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.taobao.txc.test.Simple.main(Simple.java:39) Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264)

解決方案:

把pom.xml檔案中的httpclient版本調高到4.5.3即可。

  <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>