简体   繁体   中英

Eclipse failed to load class from Jar

Eclipse JRE 4.15.0

I added stanford-corenlp-4.0.0.jar (loaded from https://stanfordnlp.github.io/CoreNLP/api.html ) to the Project/Build Path/Libraries/Classpath. When creating a CoreDocument instance

CoreDocument document = new CoreDocument("str");

Eclipse autosuggests to

import edu.stanford.nlp.pipeline.StanfordCoreNLP;

Yet during execution the import does not work:

Exception in thread "main" java.lang.NoClassDefFoundError: edu/stanford/nlp/pipeline/CoreDocument
    at main.CoreNLP.main(CoreNLP.java:35)
Caused by: java.lang.ClassNotFoundException: edu.stanford.nlp.pipeline.CoreDocument

How can I fix this inconsistency between the autosuggestion and the runtime import?

Looks like this library is not standalone. Ie it depends on some other libraries. So you cannot just add a single JAR to your project and run as, as you need to also add that JAR's dependencies and their dependencies and so on. Those dependencies are called transitive dependencies.

How do I know that? Here is what their download page says:

Stanford CoreNLP can be downloaded via the link below. This will download a large (536 MB) zip file containing (1) the CoreNLP code jar, (2) the CoreNLP models jar (required in your classpath for most tasks) (3) the libraries required to run CoreNLP , and (4) documentation / source code for the project. This is everything for getting going on English, Unzip this file. open the folder that results and you're ready to use it.

See that item #3?

One more quote:

Maven: You can find Stanford CoreNLP on Maven Central. The crucial thing to know is that CoreNLP needs its models to run (most parts beyond the tokenizer and sentence splitter) and so you need to specify both the code jar and the models jar in your pom.xml, as follows: (Note: Maven releases are usually made several days after a release on the website.)

So, the best way to use it will be to let a build tool like Gradle or Maven download all the dependencies and construct your build classpath. They have examples for Maven on that downloads page :

<dependencies>
    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>4.0.0</version>
        <classifier>models</classifier>
    </dependency>
</dependencies>

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