Java Importing Packages

Associate
Joined
18 Mar 2007
Posts
291
Hi guys,

I'm trying to get ROME: http://wiki.java.net/bin/view/Javawsxml/Rome working to read an RSS feed but am unsure as to how to import custom packages. The packages you have to download are on that page, but how do i specify them when i come to compile them?

At the moment I have this code:

Code:
package com.sun.syndication.samples;

import java.net.URL;
import java.io.InputStreamReader;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * It Reads and prints any RSS/Atom feed type.
 * <p>
 * @author Alejandro Abdelnur
 *
 */
public class FeedReader {

    public static void main(String[] args) {
        boolean ok = false;
        if (args.length==1) {
            try {
                URL feedUrl = new URL(args[0]);

                SyndFeedInput input = new SyndFeedInput();
                SyndFeed feed = input.build(new XmlReader(feedUrl));

                System.out.println(feed);

                ok = true;
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: "+ex.getMessage());
            }
        }

        if (!ok) {
            System.out.println();
            System.out.println("FeedReader reads and prints any RSS/Atom feed type.");
            System.out.println("The first parameter must be the URL of the feed to read.");
            System.out.println();
        }
    }

}

I get errors on these import statements when doing:

Code:
javac FeedReader.java &

This is when the com folder is in the same location as FeedReader.java.

How do i get it to succesfully import?
 
With the statement

Code:
package com.sun.syndication.samples;

You are effectively saying that your FeedReader class will exist in the directory ./com/sun/syndication/samples

If you want to use other classes within that structure you have to import them so something like

Code:
import com.*;
import com.sun.*;
import com.sun.syndication.*;
 
i keep getting an error saying:

Code:
package com.sun.syndication does not exist

when doing:

Code:
import com.sun.syndication.*;

The com folder is definitely in the same folder as FeedReader.java .

I have taken out the package com.sun.syndication.samples line as well.

Any ideas?
 
FeedReader.java needs to be in the ./com/sun/syndication/samples directory as you're declaring so with the package statement. So when compiling if your directory structure is something like this

directoryju6.jpg


You'd compile FeedReader with this

compilehb9.jpg
 
Hi mate,

I must be doing something wrong coz when i have it set up like:

explorerry6.jpg


and do:

cmdfh4.jpg


I'm getting that error. Maybe I have interpreted your code wrong and am not meant to just type exactly what you did?

Cheers
 
ok and what should that directory contain?

Nothing yet, when you compile with the javac -d option it's telling the compiler where to place the .class files. You just need to make sure the classes directory exists, the compiler will take care of the rest.

Then when you run the program you can specify the classpath and the java class which contains your main() method so to run it you'd type

java -cp ../classes com.sun.syndication.samples.FeedReader <------------- note the .'s
 
Last edited:
hmm, have you compiled it? i still can't get it working. where do i put the feed and io folders? when i put them in \sun\syndication it gives me a hell of a lot of errors
 
Just looking at the dependencies on the wiki page

Dependencies

* J2SE 1.4+
* JDOM 1.0

Do you have JDOM? I've no idea what it is.
 
i think that could well be the problem. will have a little research into JDOM later and also try it on my linux machine. thanks for your help!
 
Back
Top Bottom