简体   繁体   中英

Could not find or load main class — Stanford CoreNLP — Java

I'm new to java (I have Java 8) and trying to run a CoreNLP pipeline in CMD:

C:>java -Xmx5g edu.stanford.nlp.pipeline.StanfordCoreNLP -file dr19ald.txt

and keep getting:

Error: Could not find or load main class edu.stanford.nlp.pipeline.StanfordCoreNLP

I've looked through similar posts and it seems to be a classpath problem so I tried the following to no avail:

C:>java -cp "C:/stanford-corenlp-4.2.0-models-spanish.jar" edu.stanford.nlp.pipeline.StanfordCoreNLP -file dr19ald.txt

Have I added the classpath incorrectly or is there another problem I'm missing?

UPDATE: I've now tried:

C:>java -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLP -file dr19ald.txt

and

C:>java -cp "C:/*" edu.stanford.nlp.pipeline.StanfordCoreNLP -file dr19ald.txt

with the same error message.

The reference for the command line indicates that you should include more than just one jar in the classpath. They use all distributed jars (*).

Depending on your Java's version (8 or higher) you should also specify a modules directive.

edit

I further investigated the situation and it is rather messy. The distribution includes some third party libraries as well as their source code in JAR format. Other libraries are not included at all, though they are referenced in the pom.xml (and downloaded if you have the tools).

A key problem is that the JAXB Apis/Impl are not part of the Java 11 package anymore and that the code is undergoing some mutations on its way to Jakarta EE. The Java 8 still includes a JAXB implementation, though the distribution includes newer standalone files. The code has a dependency on the class com.sun.istack.FinalArrayList which is not included in the distribution but in the older com.sun.xml.bind:jaxb-core that is referenced in the POM.

Solution

You need to download the utility JAR istack-commons-runtime-3.0.7.jar (try https://mvnrepository.com/artifact/com.sun.istack/istack-commons-runtime ). I placed it in a subdirectory "m2" and referenced it in the script as REPO.

I wrote a little Windows cmd script to start the processing.

REM  run the Stanford CoreNLP on an input file 
@echo off 
SET JARS=stanford-corenlp-4.2.0.jar;stanford-corenlp-4.2.0-models.jar
SET JARS=%JARS;jollyday.jar;xom.jar;joda-time.jar;ejml-core-0.39.jar
SET JARS=%JARS%;ejml-ddense-0.39.jar;ejml-simple-0.39.jar;slf4j-api.jar
SET JARS=%JARS%;slf4j-simple.jar;protobuf.jar;javax.activation-api-1.2.0.jar
SET JARS=%JARS%;jaxb-api-2.4.0-b180830.0359.jar;jaxb-impl-2.4.0-b180830.0438.jar

SET REPO=m2
SET JARS=%JARS%;%REPO%\istack-commons-runtime-3.0.7.jar

@echo on
java -Xmx3g -cp %JARS% edu.stanford.nlp.pipeline.StanfordCoreNLP -file %1

I placed this script into the directory where I extracted the distribution ZIP.

Now I can open a Windows Command Prompt, navigate to the distribution directory and start my script, passing the input file as parameter (the %1 in the script).

Have you downloaded the various jar files into your root directory? That sounds like an organizational nightmare.

If not, though, that explains why java -cp "*" does not work. You need to figure out what subdirectory has your CoreNLP distribution and make that the directory in your classpath:

java -cp "c:\Users\john\nlp\codebase\CoreNLP\lib\*"... for example, if you're me

If you need multiple directories on a Windows machine, the correct separator is ;

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