简体   繁体   中英

Adding .jar files to classpath

I am fairly new to Java and I am trying to understand how to add .jar files to my classpath. Specifically I want to be able to import the Stanford coreNLP Library . After downloading and unzipping coreNLP, I get 4 jar files that I need to add to my classpath which are called

stanford-corenlp-3.3.1.jar
stanford-corenlp-3.3.1-models.jar
xom.jar
joda-time.jar

This SO post shows that I can do this by writing a command that includes the location of the .jar files and the path to the package hierarchy. I obviously know the location of the .jar files but I do not know what the path to the package hierarchy should be. I have tried

java -cp stanford-corenlp-3.3.1.jar;stanford-corenlp-3.3.1-models.jar;xom.jar;joda-time.jar

but this is clearly wrong because it only includes the .jar files. Can someone give me some direction on how to modify the above command? Thanks

EDIT:

The new command that I have tried is

java -cp stanford-corenlp-3.3.1.jar;stanford-corenlp-3.3.1-models.jar;xom.jar;joda-time.jar edu.stanford.nlp.pipeline.StanfordCoreNLP

as per the suggestions. However this produces the same error of

-bash: stanford-corenlp-3.3.1-models.jar: command not found
-bash: xom.jar: command not found
-bash: joda-time.jar: command not found 

Just to be clear, all I want to do is be able to use

import edu.stanford.nlp

in my java file. Also I am using a mac.

The website you link to gives several examples. You need to add:

edu.stanford.nlp.pipeline.StanfordCoreNLP

to the end of your command. This is the main class and is what will be executed when you run the program.

You can use the -cp flag as you used it but only reference the directory.

Example:

javac -cp ".;./classes;/path/to/jar/dir" MyProgram.java

Then run:

java -cp ".;./classes;/path/to/jar/dir" MyProgram

alternatively you can add the CLASSPATH as an environment variable.

In Linux/Mac you can do

export CLASSPATH=".;./classes;/PATH/TO/JARS/"

If you are using Windows you can follow instructions here to set up your environment variables.

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