Rynardt
Rynardt

Reputation: 5557

Trying to add JAR files to project, but getting NoClassDefFoundError

I downloaded the jar files from this project https://github.com/timmolter/XChange and I am now trying to get an example program running in Eclipse.

No errors are indicated before runtime, but when trying to run it I get the following error message.

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

This is my code.

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());

    }

}

From what I have read it could be a problem in the classpath, but not sure what to do. Any help would be appreciated.

Upvotes: 1

Views: 2237

Answers (4)

Connr
Connr

Reputation: 408

you have downloaded a jar that has classes that you are using in your code. The classes that you are using are in the jar that you have downloaded and put on your classpath in eclipse. At compile time your code is able to compile since the classes that your code depends on have been compiled already and can be found on the classpath (in the jar you added), therefore you do not get any compile errors.

The problem is that jar you downloaded depends on other classes that are part of other jars that you have not provided on your classpath. When you try to run, since that class isn't there you are getting the class not found exception. Like others have said I would suggest using maven so that all dependencies for a jar are pulled in when you include the one you need. You can also put all required jars on your classpath, it can just get a little hard to manage

Upvotes: 1

Sharjeel Afzal
Sharjeel Afzal

Reputation: 145

slf4j.jar is missing. download it from here and include it in classpath. http://www.slf4j.org/download.html

in Eclipse go to Project Properties->Java Build Path->Libraries then add external jar.

Upvotes: 1

user1697575
user1697575

Reputation: 2848

SLF4J lib is missing on your classpath. Download slf4j jar and add to the classpath (in Eclipse go to Project Properties->Java Build Path->Libraries then add external jar.

Upvotes: 0

Bob Flannigon
Bob Flannigon

Reputation: 1294

You're missing this jar (possibly others): org.slf4j.LoggerFactory

My recommendation would be to use Maven to manage your dependencies (via a pom), but if not just download this jar and include it with the others (i.e. on the classpath)

Upvotes: 1

Related Questions