简体   繁体   中英

How to load class from Jar?

I downloaded Jsoup and am trying to access the classes in the jar file. How can I do this? I'm using OSX. I downloaded the jsoup jar to the same directory my class is in and my include path includes ".".

In your source you import the classes during compile time using import statements like

import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element;

during runtime the classes will be loaded by the VM. (Both requires the jar file in your CLASSPATH ).

Most of the API examples omited the imports since modern IDEs insert them for you.

It depends of the level of the JRE you are using, but you typically need to actually add the jar file name to the classpath, not just the directory. So use java -cp /path/to/jsoup.jar MyClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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