tommyduarte
tommyduarte

Reputation: 41

Can't use class from maven deployed dependency

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

Upvotes: 0

Views: 826

Answers (1)

Owen Selles
Owen Selles

Reputation: 38

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

Upvotes: 2

Related Questions