简体   繁体   English

遇到找不到或无法加载主类错误的情况,有人可以帮助我修复该错误吗? 我是Java新手

[英]Getting a could not find or load main class error, can anyone help me fix it? I'm new to java

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 : 您应该在类名前面加上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. 没有它,java会在空包(当前目录)中寻找此类,并且找不到它。

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. 提供软件包时,java将在org / jsoup / examples目录中查找此类,您可以看到实际上该类文件( ListLinks.class )不在当前目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM