I am trying to write a program that parses links and other media from a webpage, and i am having an error that i don't know how to deal with popping up when i try to run the code after compiling.
package org.jsoup.examples;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.io.*;
public class ListLinks{
public static void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
File file = new File("save.txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
print(writer,"Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
Elements media = doc.select("[src]");
Elements imports = doc.select("link[href]");
print(writer,"\nMedia: (%d)", media.size());
for (Element src : media) {
if (src.tagName().equals("img"))
print(writer," * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20));
else
print(writer," * %s: <%s>", src.tagName(), src.attr("abs:src"));
}
print(writer,"\nImports: (%d)", imports.size());
for (Element link : imports) {
print(writer," * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
}
print(writer,"\nLinks: (%d)", links.size());
for (Element link : links) {
print(writer," * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}
private static void print(FileWriter writer,String msg, Object... args) {
try
{
writer.write(String.format(msg,args));
}
catch(IOException e){
System.out.println("something went wrong");
}
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
}
i am trying to run this from my command line, just parsing code from a web page.
I compiled the program by writing:
java -cp .;jsoup-1.9.2.jar ListLinks.java
then i am trying to run it by writing:
java -cp .jsoup-1.9.2.jar ListLinks
which gives me the error:
Error:could not find or load main class ListLinks
How do i fix this?
You should prepend your class name with a package org.jsoup.examples
:
java -cp .jsoup-1.9.2.jar org.jsoup.examples.ListLinks
Without it java looks for this class in empty package (current directory) and it can't find it.
When you provide a package, java will look for this class inside org/jsoup/examples directory, you can see that actually the class file ( ListLinks.class
) is located there not in the current directory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.