2013-04-12 45 views
1

我從該項目下載了這個jar文件https://github.com/timmolter/XChange,我現在試圖獲得一個在Eclipse中運行的示例程序。試圖將JAR文件添加到項目中,但獲取NoClassDefFoundError

沒有錯誤在運行前顯示,但試圖運行時,我收到以下錯誤消息。

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 
    at com.xeiam.xchange.ExchangeFactory.<init>(ExchangeFactory.java:41) 
    at com.xeiam.xchange.ExchangeFactory.<clinit>(ExchangeFactory.java:39) 
    at com.xeiam.xchange.rhbotha.bot.Main.main(Main.java:18) 
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 3 more 

這是我的代碼。

package com.xeiam.xchange.rhbotha.bot; 

import com.xeiam.xchange.Exchange; 
import com.xeiam.xchange.ExchangeFactory; 
import com.xeiam.xchange.currency.Currencies; 
import com.xeiam.xchange.dto.marketdata.Ticker; 
import com.xeiam.xchange.mtgox.v1.MtGoxExchange; 
import com.xeiam.xchange.service.marketdata.polling.PollingMarketDataService; 

public class Main { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     // Use the factory to get the version 1 MtGox exchange API using default settings 
     Exchange mtGoxExchange = ExchangeFactory.INSTANCE.createExchange(MtGoxExchange.class.getName()); 

     // Interested in the public polling market data feed (no authentication) 
     PollingMarketDataService marketDataService = mtGoxExchange.getPollingMarketDataService(); 

     // Get the latest ticker data showing BTC to USD 
     Ticker ticker = marketDataService.getTicker(Currencies.BTC, Currencies.USD); 
     System.out.println(ticker.toString()); 

     // Get the latest ticker data showing BTC to EUR 
     ticker = marketDataService.getTicker(Currencies.BTC, Currencies.EUR); 
     System.out.println(ticker.toString()); 

     // Get the latest ticker data showing BTC to GBP 
     ticker = marketDataService.getTicker(Currencies.BTC, Currencies.GBP); 
     System.out.println(ticker.toString()); 

    } 

} 

從我讀過的內容可能是類路徑中的問題,但不知道該怎麼做。任何幫助,將不勝感激。

+0

該項目有一個pom.xml。使用maven來管理依賴關係或使用項目wiki上的[依賴信息](https://github.com/timmolter/XChange)來管理它。我會推薦前者。 –

+0

如何在Eclipse中使用pom.xml文件? – Rynardt

+0

使用[m2eclipse](http://www.sonatype.org/m2eclipse/)插件。 –

回答

1

你錯過了這個罐子(可能還有其他):org.slf4j.LoggerFactory

我的建議是使用Maven管理依賴關係(通過POM),但如果不只是下載這個jar和包括它與其他人(即在類路徑上)

+0

如何在Eclipse中使用pom.xml文件? – Rynardt

+0

http://maven.apache.org/index.html –

0

SLF4J庫在類路徑中缺失。下載slf4j jar並添加到類路徑(在Eclipse中轉到項目屬性 - > Java構建路徑 - >庫,然後添加外部JAR。

1

slf4j.jar缺失。從這裏下載並將其包含在類路徑中。 http://www.slf4j.org/download.html

在Eclipse轉到項目屬性 - > Java構建路徑 - >庫,然後添加外部JAR。

1

你已經下載具有您正在使用您的代碼類的jar包,您正在使用的類是在你已經下載並放在你的類路徑中的jar在編譯時你的代碼能夠編譯,因爲你的代碼依賴的類n已經被編譯好了,並且可以在類路徑中找到(在你添加的jar中),因此你不會遇到任何編譯錯誤。

問題是,您下載的jar取決於其他類,這些類是您在類路徑中沒有提供的其他jar的一部分。當你嘗試運行時,因爲那個類不在那裏,你得到的類沒有找到異常。就像其他人所說的,我會建議使用maven,以便在包含所需的jar包時,將所有jar的依賴關係都拉進來。您也可以將所有必需的jar包放在classpath中,它可以稍微難以管理

相關問題