无法在简单的Java Soup应用程序上找到或加载主类 [英] Could not find or load main class on simple Java Soup app

查看:124
本文介绍了无法在简单的Java Soup应用程序上找到或加载主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处

进行了这个简单程序的编译

package com.stackoverflow.q2835505;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

public static void main(String[] args) throws Exception {
    String url = "https://stackoverflow.com/questions/2835505";
    Document document = Jsoup.connect(url).get();

    String question = document.select("#question .post-text").text();
    System.out.println("Question: " + question);

    Elements answerers = document.select("#answers .user-details a");
    for (Element answerer : answerers) {
        System.out.println("Answerer: " + answerer.text());
    }
}

}

在终端中使用以下命令:

javac -cp ./jsoup-1.10.2.jar Test.java

但是当我尝试运行它时,我会这样做:

错误:找不到或加载主类

我找不到解决方案,问题出在哪里?谢谢.

解决方案

您可能在这里遇到了多个问题...

Javac .可以肯定的是,像这样编译Java应用程序:

 javac -cp ./jsoup-1.10.2.jar -d . Test.java
 

-d选项可确保将已​​编译的类放在相应的 package 目录中:

com/stackoverflow/q2835505/Test.class

,而不是当前目录上.让我们检查手册页只是确定(-d选项):

设置类文件的目标目录.该目录必须已经存在,因为javac不会创建它.如果类是包的一部分,则javac将类文件放在反映包名称的子目录中,并根据需要创建目录.

如果未指定-d选项 ,则javac会将每个类文件放在与其生成源文件相同的目录中.

Java .最后,使用以下命令运行它:

 java -cp .:./jsoup-1.10.2.jar com.stackoverflow.q2835505.Test
 

这将使用当前目录(.)和jsoup-1.10.2.jar作为类路径来运行您的应用程序.当前目录为强制性,因此java会找到您的Test.class以及JSoup jar.


有关java命令语法的更多信息,请参见这个不错的答案.

I do the compilation of this simple programm i found here

package com.stackoverflow.q2835505;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Test {

public static void main(String[] args) throws Exception {
    String url = "https://stackoverflow.com/questions/2835505";
    Document document = Jsoup.connect(url).get();

    String question = document.select("#question .post-text").text();
    System.out.println("Question: " + question);

    Elements answerers = document.select("#answers .user-details a");
    for (Element answerer : answerers) {
        System.out.println("Answerer: " + answerer.text());
    }
}

}

with this command in terminal:

javac -cp ./jsoup-1.10.2.jar Test.java

but when i try to run it i take this:

Error:Could not find or load main class

and I can't find the solution, where is the problem? Thanks.

解决方案

You might be running into more than one issue here...

Javac. To be sure, compile your Java app like this:

javac -cp ./jsoup-1.10.2.jar -d . Test.java

the -d option ensures that the compiled class is placed in the corresponding package directory:

com/stackoverflow/q2835505/Test.class

and not on your current directory. Let's check the man page just to be sure (-d option):

Sets the destination directory for class files. The directory must already exist because javac does not create it. If a class is part of a package, then javac puts the class file in a subdirectory that reflects the package name and creates directories as needed.

If the -d option is not specified, then javac puts each class file in the same directory as the source file from which it was generated.

Java. Finally, run it using:

java -cp .:./jsoup-1.10.2.jar com.stackoverflow.q2835505.Test

this runs your app using your current directory (.) and jsoup-1.10.2.jar as your class path. The current directory is mandatory so java finds your Test.class as well as the JSoup jar.


See this nice answer for a lot more information on the java command syntax.

这篇关于无法在简单的Java Soup应用程序上找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆