0

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.

Community
  • 1
  • 1
user1893354
  • 5,778
  • 12
  • 46
  • 83
  • What you have is all you need (to set the classpath). But of course, you need a main class to execute. – JB Nizet Feb 25 '14 at 22:09
  • You deal with the package hierarchies in your code, as packages. – helderdarocha Feb 25 '14 at 22:09
  • Can you comment on the command that I provided? It is not correct because it creates errors i.e. `file_name.jar: command not found` – user1893354 Feb 25 '14 at 22:13
  • You didn't provide any command containing file_name.jar. What do you want to execute, and what complete command are you using, and on which OS? – JB Nizet Feb 25 '14 at 22:36
  • I was just using file_name.jar as a placeholder. The actual errors were `bash: stanford-corenlp-3.3.1-models.jar: command not found -bash: xom.jar: command not found -bash: joda-time.jar: command not found`. – user1893354 Feb 26 '14 at 13:44

2 Answers2

0

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.

martinez314
  • 12,162
  • 5
  • 36
  • 63
  • This produces an error that I have shown in my edit. Any idea what is going on? – user1893354 Feb 26 '14 at 13:58
  • @user1893354 Since you are running this in a Unix-like environment, you need to use colons, not semicolons. Replace all the semicolons with colons. – martinez314 Feb 26 '14 at 14:53
0

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.

user2537361
  • 212
  • 1
  • 3