我正在尝试使用Scala代码中的caldav4j
与在线日历进行交互。
我已经从caldav4j
示例中复制/粘贴了代码,以检查项目设置是否正确,但是当我尝试运行代码时,遇到关于javax
的缓存的错误。
代码如下:
import com.github.caldav4j._
import com.github.caldav4j.methods._
import org.slf4j._
import org.apache.http.impl.client.HttpClients
import collection.JavaConverters._
import scala.io.Source
import net.fortuna.ical4j._
import net.fortuna.ical4j.model._
import net.fortuna.ical4j.data._
object Main extends App {
def caldav4j(uri: String) = {
val factory = new CalDAV4JMethodFactory();
val method = factory.createGetMethod(uri);
val client = HttpClients.createDefault();
// Execute the method.
val response = client.execute(method);
// Retrieve the Calendar from the response.
val calendar = method.getResponseBodyAsCalendar(response);
println(calendar.toString)
}
val uri = "url"
caldav4j(uri)
}
这是我的build.sbt
文件:
libraryDependencies ++= Seq(
"com.github.caldav4j" % "caldav4j" % "1.0.0-rc.2",
"com.github.lookfirst" % "sardine" % "5.9",
"org.slf4j" % "slf4j-api" % "1.7.5",
"org.slf4j" % "slf4j-simple" % "1.7.5",
"javax.xml.bind" % "jaxb-api" % "2.2.11",
"com.sun.xml.bind" % "jaxb-core" % "2.2.11",
"com.sun.xml.bind" % "jaxb-impl" % "2.2.11",
"javax.activation" % "activation" % "1.1.1",
"org.mnode.ical4j" % "ical4j" % "3.0.0",
"javax.cache" % "cache-api" % "1.0.0",
// "org.jsr107.ri" % "cache-ri-impl" % "0.8"
)
在最后一行加上注释后,出现此错误:
[error] (run-main-5) javax.cache.CacheException: No CachingProviders have been configured
[error] javax.cache.CacheException: No CachingProviders have been configured
[error] at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:381)
[error] at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:351)
[error] at javax.cache.Caching.getCachingProvider(Caching.java:142)
[error] at net.fortuna.ical4j.util.JCacheTimeZoneCache.<init>(JCacheTimeZoneCache.java:15)
[error] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[error] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
[error] at java.base/java.lang.Class.newInstance(Class.java:560)
[error] at net.fortuna.ical4j.model.TimeZoneLoader.lambda$cacheInit$1(TimeZoneLoader.java:280)
[error] at java.base/java.util.Optional.orElseGet(Optional.java:358)
[error] at net.fortuna.ical4j.model.TimeZoneLoader.cacheInit(TimeZoneLoader.java:278)
[error] at net.fortuna.ical4j.model.TimeZoneLoader.<init>(TimeZoneLoader.java:84)
[error] at net.fortuna.ical4j.model.TimeZoneRegistryImpl.<init>(TimeZoneRegistryImpl.java:125)
[error] at net.fortuna.ical4j.model.TimeZoneRegistryImpl.<init>(TimeZoneRegistryImpl.java:116)
[error] at net.fortuna.ical4j.model.DefaultTimeZoneRegistryFactory.createRegistry(DefaultTimeZoneRegistryFactory.java:48)
[error] at net.fortuna.ical4j.data.CalendarBuilder.<init>(CalendarBuilder.java:105)
[error] at com.github.caldav4j.methods.CalDAV4JMethodFactory.getCalendarBuilderInstance(CalDAV4JMethodFactory.java:398)
[error] at com.github.caldav4j.methods.CalDAV4JMethodFactory.createGetMethod(CalDAV4JMethodFactory.java:300)
[error] at Main$.caldav4j(main.scala:29)
[error] at Main$.delayedEndpoint$Main$1(main.scala:51)
[error] at Main$delayedInit$body.apply(main.scala:16)
[error] at scala.Function0.apply$mcV$sp(Function0.scala:39)
[error] at scala.Function0.apply$mcV$sp$(Function0.scala:39)
[error] at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
[error] at scala.App.$anonfun$main$1$adapted(App.scala:80)
[error] at scala.collection.immutable.List.foreach(List.scala:392)
[error] at scala.App.main(App.scala:80)
[error] at scala.App.main$(App.scala:78)
[error] at Main$.main(main.scala:16)
[error] at Main.main(main.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:564)
寻找该错误的解决方案时,我发现我需要添加javax.cache
接口的实现。建议的实现是build.sbt
中注释行中的实现,但是当我取消注释时,会出现此错误:
[error] (run-main-6) java.lang.AbstractMethodError: org.jsr107.ri.RICacheManager.createCache(Ljava/lang/String;Ljavax/cache/configuration/Configuration;)Ljavax/cache/Cache;
[error] java.lang.AbstractMethodError: org.jsr107.ri.RICacheManager.createCache(Ljava/lang/String;Ljavax/cache/configuration/Configuration;)Ljavax/cache/Cache;
[error] at net.fortuna.ical4j.util.JCacheTimeZoneCache.<init>(JCacheTimeZoneCache.java:23)
[error] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[error] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
[error] at java.base/java.lang.Class.newInstance(Class.java:560)
[error] at net.fortuna.ical4j.model.TimeZoneLoader.lambda$cacheInit$1(TimeZoneLoader.java:280)
[error] at java.base/java.util.Optional.orElseGet(Optional.java:358)
[error] at net.fortuna.ical4j.model.TimeZoneLoader.cacheInit(TimeZoneLoader.java:278)
[error] at net.fortuna.ical4j.model.TimeZoneLoader.<init>(TimeZoneLoader.java:84)
[error] at net.fortuna.ical4j.model.TimeZoneRegistryImpl.<init>(TimeZoneRegistryImpl.java:125)
[error] at net.fortuna.ical4j.model.TimeZoneRegistryImpl.<init>(TimeZoneRegistryImpl.java:116)
[error] at net.fortuna.ical4j.model.DefaultTimeZoneRegistryFactory.createRegistry(DefaultTimeZoneRegistryFactory.java:48)
[error] at net.fortuna.ical4j.data.CalendarBuilder.<init>(CalendarBuilder.java:105)
[error] at com.github.caldav4j.methods.CalDAV4JMethodFactory.getCalendarBuilderInstance(CalDAV4JMethodFactory.java:398)
[error] at com.github.caldav4j.methods.CalDAV4JMethodFactory.createGetMethod(CalDAV4JMethodFactory.java:300)
[error] at Main$.caldav4j(main.scala:29)
[error] at Main$.delayedEndpoint$Main$1(main.scala:51)
[error] at Main$delayedInit$body.apply(main.scala:16)
[error] at scala.Function0.apply$mcV$sp(Function0.scala:39)
[error] at scala.Function0.apply$mcV$sp$(Function0.scala:39)
[error] at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
[error] at scala.App.$anonfun$main$1$adapted(App.scala:80)
[error] at scala.collection.immutable.List.foreach(List.scala:392)
[error] at scala.App.main(App.scala:80)
[error] at scala.App.main$(App.scala:78)
[error] at Main$.main(main.scala:16)
[error] at Main.main(main.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:564)
如何解决此错误?