簡體   English   中英

遇到找不到或無法加載主類錯誤的情況,有人可以幫助我修復該錯誤嗎? 我是Java新手

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

我正在嘗試編寫一個程序來解析網頁中的鏈接和其他媒體,並且遇到一個錯誤,當我嘗試在編譯后運行代碼時,我不知道該如何處理彈出窗口。

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

}

我試圖從命令行運行此命令,只是從網頁中解析代碼。

我通過編寫以下程序來編譯程序:

java -cp .;jsoup-1.9.2.jar ListLinks.java

然后我試圖通過編寫來運行它:

java -cp .jsoup-1.9.2.jar ListLinks

這給了我錯誤:

Error:could not find or load main class ListLinks

我該如何解決?

您應該在類名前面加上org.jsoup.examples包:

java -cp .jsoup-1.9.2.jar org.jsoup.examples.ListLinks

沒有它,java會在空包(當前目錄)中尋找此類,並且找不到它。

提供軟件包時,java將在org / jsoup / examples目錄中查找此類,您可以看到實際上該類文件( ListLinks.class )不在當前目錄中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM