By tommyduarte


2018-12-06 15:04:09 8 Comments

I deployed my dependency to a private maven repository (JFrog Artifactory) and when I'm going to test it throws me this error "Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.util.PropertiesUtil"

This is what I am calling:

AccountManager.getInstance().getAccounts();

In this account manager, I have an account service which makes requests to my backend API using the restTemplate from spring boot.

Stacktrace:

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.util.PropertiesUtil
    at org.apache.logging.log4j.status.StatusLogger.<clinit>(StatusLogger.java:78)
    at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:60)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:135)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:102)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:79)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.springframework.http.HttpLogging.<clinit>(HttpLogging.java:45)
    at org.springframework.http.client.support.HttpAccessor.<init>(HttpAccessor.java:48)
    at org.springframework.http.client.support.InterceptingHttpAccessor.<init>(InterceptingHttpAccessor.java:44)
    at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:138)
    at br.com.toxicnetwork.tooling.account.AccountServiceImpl.<init>(AccountServiceImpl.java:17)
    at br.com.toxicnetwork.tooling.account.AccountManager.<init>(AccountManager.java:16)
    at br.com.toxicnetwork.tooling.account.AccountManager.getInstance(AccountManager.java:71)
    at br.com.toxicnetwork.engine.Main.main(Main.java:8)

Account Manager:

public class AccountManager {

private static AccountManager instance;
private AccountService accountService;
private Map<String, Account> cachedAccounts;

private AccountManager() {
    accountService = new AccountServiceImpl();
    cachedAccounts = new WeakHashMap<>();
}

/**
 * Retrieves all cached accounts
 *
 * @return all accounts created on crm
 */
public Collection<Account> getAccounts() {
    return cachedAccounts.values();
}

/**
 * Create a new account
 *
 * @param account account
 * @return if the account already exists it returns as null, if not returns a newly account
 */
public Account create(Account account) {
    Account createdAccount = accountService.create(account);
    cachedAccounts.putIfAbsent(createdAccount.getId(), createdAccount);

    return createdAccount;
}

/**
 * Gets the account by it's id and deletes
 *
 * @param id account id
 * @return account deleted
 */
public Account delete(String id) {
    cachedAccounts.computeIfPresent(id, (presentId, account) -> cachedAccounts.remove(presentId));
    return accountService.delete(id);
}

/**
 * Get the account by it's id
 *
 * @param id account id
 * @return account found by id
 */
public Account getAccountById(String id) {
    return cachedAccounts.containsKey(id) ? cachedAccounts.get(id) : accountService.getAccountById(id);
}

public void setAccountService(AccountService accountService) {
    this.accountService = accountService;
}

public static AccountManager getInstance() {
    if (instance == null) {
        synchronized (AccountManager.class) {
            if (instance == null) {
                instance = new AccountManager();
            }
        }
    }
    return instance;
}
}

Account Service:

public class AccountServiceImpl implements AccountService {

private RestTemplate restTemplate;

public AccountServiceImpl() {
    restTemplate = new RestTemplate();
}

@Override
public List<Account> getAccounts(){
    ResponseEntity<List<Account>> response = restTemplate.exchange(
            Endpoints.USERS.getUrl(),
            HttpMethod.GET,
            null,
            new ParameterizedTypeReference<List<Account>>() {
            });

    List<Account> accounts = response.getBody();

    if(accounts == null){
        return null;
    }

    return accounts;
}

@Override
public Account create(Account account) {
    return restTemplate.postForObject(Endpoints.CREATE_USER.getUrl(), account, Account.class);
}

@Override
public Account delete(String id) {
    Account toDelete = getAccountById(id);

    if(toDelete == null){
        return null;
    }

    restTemplate.delete(Endpoints.DELETE_USER_BY_ID.getUrl(), toDelete.getId(), id);

    return toDelete;
}

@Override
public Account getAccountById(String id) {
    return restTemplate.getForObject(Endpoints.USERS_BY_ID.getUrl(), Account.class, id);
}
}

1 comments

@Owen Selles 2018-12-06 15:08:08

Log4j has an issue there which is reported at https://issues.apache.org/jira/browse/LOG4J2-2129. Either use 2.9, or use 2.10 with the workaround that's in that ticket or use 2.11.1

@tommyduarte 2018-12-06 15:21:41

The thing is... Im not using Log4j directly as dependency, im using the spring-boot-starter

@Owen Selles 2018-12-06 15:25:25

Can you add your stacktrace and code snippet to the question?

@tommyduarte 2018-12-06 15:37:45

Done, that might help u

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Dealing with "Xerces hell" in Java/Maven?

25 Answered Questions

[SOLVED] Java inner class and static nested class

  • 2008-09-16 08:22:35
  • Omnipotent
  • 657619 View
  • 1541 Score
  • 25 Answer
  • Tags:   java inner-classes

35 Answered Questions

[SOLVED] Create ArrayList from array

33 Answered Questions

7 Answered Questions

[SOLVED] What exactly is a Maven Snapshot and why do we need it?

27 Answered Questions

[SOLVED] How to add local jar files to a Maven project?

18 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 708309 View
  • 1930 Score
  • 18 Answer
  • Tags:   java constructor

12 Answered Questions

[SOLVED] How do I tell Maven to use the latest version of a dependency?

21 Answered Questions

[SOLVED] Force maven update

  • 2011-01-15 19:01:41
  • M4ks
  • 668565 View
  • 584 Score
  • 21 Answer
  • Tags:   maven

9 Answered Questions

Sponsored Content