2

I'm trying to add bank holidays from an ics calendar to an ArrayList of Date objects:

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

However, I always get:

Exception in thread "main" java.lang.NoClassDefFoundError: net/fortuna/ical4j/data/ParserException at framework.GPSIS.main(GPSIS.java:29) Caused by: java.lang.ClassNotFoundException: net.fortuna.ical4j.data.ParserException

I have the imports in the beginning of the file:

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

And my .classpath contains:

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

I'm fairly new to Eclipse, and I'm trying to figure out what I'm missing here.

Milka
  • 297
  • 2
  • 11

1 Answers1

3

The catch block for ParserException is actually malformed. The pipe | is usually used to catch several different exceptions in the same block (because you would handle them in the same way), and not twice the same exception:

catch(ParserException | ParserException e)

What happens here, is the java runtime matches the first ParserException against the import from ical4j, then looks for another ParserException to match the second one and doesn't find any.

b2Wc0EKKOvLPn
  • 2,054
  • 13
  • 15
  • Seriously? If the Java runtime is going to do something like that, then this should be a compile error. Definitely a bug in javac. Do Oracle know about it? Does someone need to report it? – Dawood ibn Kareem Mar 13 '14 at 03:46
  • @DavidWallace well if you have another explanation ... please do share :-). – b2Wc0EKKOvLPn Mar 14 '14 at 01:30
  • No, I think you're probably right. I upvoted you after all. I just asked if Oracle know about this bug (assuming you're correct). – Dawood ibn Kareem Mar 14 '14 at 02:11
  • Well I don't think it's a bug. It's actually pretty consistent behaviour. At compile time it passes because the compiler finds twice a reference for a class it has an import for and it can resolve. But when running, the runtime is expecting a number different exceptions. And it's not a bug because the language cannot possibly be bullet-proof. At some point it's the responsibility of the developer to read and understand the document and use the language features appropriately. – b2Wc0EKKOvLPn Mar 14 '14 at 02:17
  • I strongly disagree. If there is code that is guaranteed to give an `Error` (not an `Exception`) at runtime, the compiler should be able to catch it. – Dawood ibn Kareem Mar 14 '14 at 02:19
  • Well it's a debatable question. And from the standpoint of languages like java it might be necessary. But as far as preferences go, I do prefer minimal concise languages where the programmer is trusted to know what he does. And as such I tend to prefer less clutter in the compiler (or the language infrastructure in general). – b2Wc0EKKOvLPn Mar 14 '14 at 02:26