java.lang.ExceptionInInitializerError when creatin

2019-02-20 23:31发布

I am trying to use ehCache in Android, and getting the following error

java.lang.ExceptionInInitializerError
    at net.sf.ehcache.EhcacheDefaultClassLoader.getInstance(EhcacheDefaultClassLoader.java:29)
    at net.sf.ehcache.config.Configuration.<init>(Configuration.java:208)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:152)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103)
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:140)
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:892)
    at net.sf.ehcache.CacheManager.create(CacheManager.java:873)
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:907)

Caused by: java.lang.NullPointerException: parentLoader == null && !nullAllowed
    at java.lang.ClassLoader.<init>(ClassLoader.java:210)
    at java.lang.ClassLoader.<init>(ClassLoader.java:202)
    at net.sf.ehcache.EhcacheDefaultClassLoader.<init>(EhcacheDefaultClassLoader.java:35)
    at net.sf.ehcache.EhcacheDefaultClassLoader.<clinit>(EhcacheDefaultClassLoader.java:26)
    at net.sf.ehcache.EhcacheDefaultClassLoader.getInstance(EhcacheDefaultClassLoader.java:29) 
    at net.sf.ehcache.config.Configuration.<init>(Configuration.java:208) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:152) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103) 
    at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:140) 
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:892) 
    at net.sf.ehcache.CacheManager.create(CacheManager.java:873) 
    at net.sf.ehcache.CacheManager.getInstance(CacheManager.java:907)

here is my code where I am trying to initialize

private static Cache getCache(String cacheName) throws IllegalStateException{
    CacheManager cacheManager = CacheManager.getInstance();
    Cache cache;
    if(!cacheManager.cacheExists(cacheName)) {
        cacheManager.addCache(cacheName);
    }
    cache = cacheManager.getCache(cacheName);
    return cache;
}

Looks like EHCache won't work in android? Can anybody put some light on this? After I got the error, I have also put a ehcache.xml with some configuration inside res/xml/ The ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  monitoring="autodetect" dynamicConfig="true">

    <!-- By default, Ehcache stored the cached files in temp folder. -->    <!-- <diskStore path="java.io.tmpdir" /> -->

    <!-- Ask Ehcache to store cache in this path -->    <!--<diskStore path="c:\\cache" /> -->

    <!-- Sample cache named cache1
    This cache contains a maximum in memory of 10000 elements, and will expire
    an element if it is idle for more than 5 minutes and lives for more than
    10 minutes.

    If there are more than 10000 elements it will overflow to the
    disk cache, which in this configuration will go to wherever java.io.tmp is
    defined on your system. On a standard Linux system this will be /tmp" 
    -->     
    <cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" transactionalMode="off">         
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

1条回答
啃猪蹄的小仙女
2楼-- · 2019-02-21 00:13

Ehcache is not able to locate the ehcach.xml file. It has no idea about res/xml folder.

So try to configure your cache programmatically in this way (ehcache 2.10):

// create default CacheManager
Configuration config = new Configuration();
config.setName("Mngmt");
// [...]
CacheManager cacheManager = CacheManager.create(config);

int maxEntriesLocalHeap = 100;
String cachName = "cacheName";
Cache cache = new Cache(
  new CacheConfiguration(cachName, maxEntriesLocalHeap)
    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
    .eternal(false)
    .timeToLiveSeconds(120)
    .timeToIdleSeconds(60)
    .diskExpiryThreadIntervalSeconds(0));
cacheManager.addCache(cache);

More Information you will find here: Creating Caches Programmatically

查看更多
登录 后发表回答