【问题标题】:ParserException with ical4j in EclipseEclipse 中带有 ical4j 的 ParserException
【发布时间】:2014-03-13 02:32:46
【问题描述】:

我正在尝试将 ics 日历中的银行假期添加到 Date 对象的 ArrayList:

    public void loadHolidays()
{
    try {
        URL holidays = new URL("https://www.gov.uk/bank-holidays/england-and-wales.ics");
        InputStream fin = holidays.openStream();

        CalendarBuilder builder = new CalendarBuilder();
        Calendar calendar = builder.build(fin);

        for (Iterator<?> i = calendar.getComponents().iterator(); i.hasNext();) {
            Component component = (Component) i.next();
            SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
            publicHolidays.add(fm.parse(component.getProperty("DTSTART").getValue()));                  
        }

        System.out.println("\t\tSuccess.");
    } catch (IOException e) {
        System.out.println("\t\tFailed. www.gov.uk/bank-holidays/england-and-wales.ics does not exist.");
    } catch (ParserException | ParseException e) {
        System.out.println("\t\tFailed. Format changed in iCalendar");
    } 
}

但是,我总是得到:

线程“主”java.lang.NoClassDefFoundError 中的异常:net/fortuna/ical4j/data/ParserException 在 framework.GPSIS.main(GPSIS.java:29) 引起:java.lang.ClassNotFoundException:net.fortuna.ical4j.data.ParserException

我在文件的开头有导入:

import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Component;

我的 .classpath 包含:

<classpathentry exported="true" kind="lib" path="library/ical4j-1.0.5.jar"/>

我对 Eclipse 还很陌生,我正试图弄清楚我在这里缺少什么。

【问题讨论】:

  • +1 表示有据可查的问题。 Project / Properties / Order and Export,列出的依赖项是什么(按它们出现的顺序)。
  • 尝试将项目 MilkaNewBranch 移​​动到该列表的底部并再次尝试查看您是否有 ClassNotFoundException。
  • 将其移到底部但仍有 ClassNotFoundException
  • 这种情况发生在编译时还是运行时?

标签: java eclipse parsing exception ical4j


【解决方案1】:

ParserException 的 catch 块实际上格式不正确。管道| 通常用于在同一块中捕获多个不同的异常(因为您会以相同的方式处理它们),而不是两次相同的异常:

catch(ParserException | ParserException e)

这里发生的情况是,java 运行时将第一个 ParserException 与从 ical4j 的导入匹配,然后寻找另一个 ParserException 来匹配第二个,但没有找到。

【讨论】:

  • 认真的吗?如果 Java 运行时会做类似的事情,那么这应该是一个编译错误。绝对是javac中的一个错误。甲骨文知道吗?有人需要举报吗?
  • @DavidWallace 好吧,如果你有其他解释......请分享:-)。
  • 不,我认为您可能是对的。毕竟我给你投了赞成票。我只是问 Oracle 是否知道这个错误(假设你是对的)。
  • 嗯,我不认为这是一个错误。这实际上是非常一致的行为。在编译时它通过了,因为编译器找到了两次它具有导入的类的引用并且它可以解析。但是在运行时,运行时会出现许多不同的异常。这不是一个错误,因为该语言不可能是防弹的。在某些时候,开发人员有责任阅读和理解文档并适当地使用语言功能。
  • 我强烈反对。如果有代码保证在运行时给出Error(而不是Exception),编译器应该能够捕获它。
猜你喜欢
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
相关资源
最近更新 更多