在Eclipse中使用ical4j进行ParserException

时间:2014-03-13 02:32:47

标签: java eclipse parsing exception ical4j

我正在尝试将银行假日从日历日历添加到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");
    } 
}

然而,我总是得到:

  

线程“main”中的异常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 个答案:

答案 0 :(得分:3)

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

catch(ParserException | ParserException e)

这里发生的是,java运行时匹配第一个ParserException与ical4j的导入,然后查找另一个ParserException以匹配第二个并且找不到任何。