Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac User - How do I set CLASSPATHS in Mac (I'm working on a Lucene Demo)

I'm trying to get my Apache Lucene demo to work and I'm down to setting the classpath in this tutorial http://lucene.apache.org/java/2_3_2/demo.html

I've hunted the web and these wer the 2 solutions I found to set CLASSPATH:

CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar

and

setenv CLASSPATH ${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar 

The second one brings up a error -bash: setenv: command not found

The first one seemed to accept ok but wen i tried the next step in the tutorial i got an error. The next step was to run the following:

Phil-hunters-MacBook:webapps philhunter$ java org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src

which gave me the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles

This leads me to believe my CLASSPATHS didnt set correctly. Would I be right in assuming this? I have tried other tutorials and demos and see to get this same error quite a bit. Im new to Lucene and relatively new to mac and Unix shell scripting. Anyone know if I am setting the CLASSPATH correctly and if thats the cause of the errors?

like image 225
Phil Hunter Avatar asked Feb 01 '11 23:02

Phil Hunter


3 Answers

in the terminal type

$ vim ~/.bash_profile

edit the file and add one line:

export CLASSPATH=${CLASSPATH}:/usr/local/lucene-3.6.2/lucene-core-3.6.2.jar:/usr/local/lucene-3.6.2/contrib/demo/lucene-demo-3.6.2.jar;

make sure to change the path of yours.

In your way you lose to add lucene-demo-3.0.3.jar in your classpath.

like image 168
malajisi Avatar answered Nov 07 '22 19:11

malajisi


When you set an environment variable like CLASSPATH then by default it only applies to the current process (i.e. the shell process itself) - it isn't available to the java process you launch in the next line. In order to make it available to other processes you need to "export" the variable. In this case you can use something like:

export CLASSPATH=${CLASSPATH}:/Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar

This basically says "set the CLASSPATH variable to its current value plus the location of the lucene jar, and make the new variable available to any processes launched from this shell".

However, with java the usual way of setting the classpath is to do it as part of the java command itself, using the -classpath or -cp options. In your case it would look something like:

Phil-hunters-MacBook:webapps philhunter$ java -cp /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/lucene-core-3.0.3.jar org.apache.lucene.demo.IndexFiles /Users/philhunter/Desktop/COM562\ Project/lucene-3.0.3/src

As an aside, the error you see when using the setenv line is because setenv is the command used in the C shell to set environment variables, but the default Mac shell (and the shell you're using) is bash which doesn't recognise setenv and lets you know it doesn't recognise it with the error message: -bash: setenv: command not found.

like image 35
matt Avatar answered Nov 07 '22 19:11

matt


i create a .bash_profile file in my home directory and do things like

export GRAILS_HOME=/usr/share/grails
...
export PATH=${GRAILS_HOME}/bin:${GROOVY_HOME}/bin:/usr/local/mysql-5.1.45-osx10.6-x86_64/bin:${PATH}

you can work of that to set the classpath -- these examples show how to declare an environment variable and how to use the variable in other variables.

like image 24
hvgotcodes Avatar answered Nov 07 '22 19:11

hvgotcodes