soup java,无法在简单的Java Soup应用程序上找到或加载主类

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值