[英]How to use external jar dependency in Java program?
I'm having trouble figuring out how to use JSoup in my program.我无法弄清楚如何在我的程序中使用 JSoup。 I figured you simply needed to have the.jar file in the same project folder but this is not the case.我想你只需要在同一个项目文件夹中拥有 .jar 文件,但事实并非如此。 There is very little info on the subject online.网上关于这个主题的信息很少。
Assuming you have the following directory structure假设您具有以下目录结构
.
├── Main.java
└── jsoup-1.12.1.jar
and this content in your Main.java
以及您Main.java
中的此内容
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Main {
public static void main(String[] args) throws IOException {
String url = "http://www.google.com";
Document doc = Jsoup.connect(url).get();
String title = doc.title();
System.out.println(title);
}
}
In order to be able to compile and run your program, you need to have your jar
in your classpath
.为了能够编译和运行你的程序,你需要在你的classpath
中有你的jar
。
Compile your program like this像这样编译你的程序
javac -cp .:jsoup-1.12.1.jar Main.java
And run it like this像这样运行它
java -cp .:jsoup-1.12.1.jar Main
I think this is a good reference point to start learning about Java's classpath
我认为这是开始学习 Java classpath
的一个很好的参考点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.