I use eclipse to write RSS parser wrapper function and it returns expe开发者_StackOverflow社区cted values when run from eclipse.
package rss_parser_wrapper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import com.sun.cnpi.rss.elements.Item;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserException;
import com.sun.cnpi.rss.parser.RssParserFactory;
public class RssParserWrapper {
public static Rss getRss(String url) throws RssParserException, MalformedURLException, IOException {
RssParser parser = RssParserFactory.createDefault();
Rss rss = parser.parse(new URL(url));
return rss;
}
}
So, now I need to compile this in order to push to the tomcat web server.
I tried to compile from directory from my current dir (classes):
javac -cp . -target 1.5 -source 1.5 rss_parser_wrapper/RssParserWrapper.java
I get the error:
makun /home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first/WEB-INF/classes ->javac -cp . -source 1.5 -target 1.5 rss_parser_wrapper/RssParserWrapper.java
rss_parser_wrapper/RssParserWrapper.java:9: package com.sun.cnpi.rss.elements does not exist
import com.sun.cnpi.rss.elements.Channel;
^
rss_parser_wrapper/RssParserWrapper.java:10: package com.sun.cnpi.rss.elements does not exist
import com.sun.cnpi.rss.elements.Item;
^
rss_parser_wrapper/RssParserWrapper.java:11: package com.sun.cnpi.rss.elements does not exist
import com.sun.cnpi.rss.elements.Rss;
^
rss_parser_wrapper/RssParserWrapper.java:12: package com.sun.cnpi.rss.parser does not exist
import com.sun.cnpi.rss.parser.RssParser;
^
rss_parser_wrapper/RssParserWrapper.java:13: package com.sun.cnpi.rss.parser does not exist
Does that mean Java class library does not have those classes? How come it does not complain when I run the file from eclipse?
You need to add the path to JAR file containing the RSS library to the classpath.
javac -cp .:/path/to/rss.jar -target 1.5 -source 1.5 rss_parser_wrapper/RssParserWrapper.java
It works in Eclipse because it's smart enough to include the necessary libraries in the classpath (based on the configured project's buildpath).
精彩评论