• Coding
  • Setting java classpath using .sh file

I am having a problem in setting the classpath to the libraries of the CRF program that I
have downloaded. There is a .sh file that I have to run, but first I have to set the
CRF_HOME to the directory to where I have the program. I have been trying to do it but it
is not working. Any help is appreciated. This is the .sh file.
# settings for CRF..
export CLASSPATH=$CRF_HOME/lib/CRF.jar:$CRF_HOME/lib/colt.jar:$CRF_HOME/lib/LBFGS.jar:$CLASSPATH
The README file says this:
Make sure to set the classpath by running "settings.sh" after
setting CRF_HOME to the directory where you have unpackaged this distribution.

I tried this before running it, but it did not work.
# settings for CRF..
CRF_HOME=/Desktop/Workspace/CRF
export CLASSPATH=$CRF_HOME/lib/CRF.jar:$CRF_HOME/lib/colt.jar:$CRF_HOME/lib/LBFGS.jar:$CLASSPATH
We need much more information in order to help you:

1- What OS are you using?
2- Are you getting an error message? "did not work" is not really useful.
3- "Tried this before running". Can you please describe better what the steps are? The correct way should be to write this in a terminal then launch your java program from the same terminal. If you double clicked on an icon or something, you need another way to set up your environment.

Edit I'm guessing you're on OS X. But please if you could give us the error message and describe the steps you're following it would help.

Edit2: I think the problem is that your variable CRF_HOME is not sent to the java process. Try doing the follwing:
# settings for CRF..
export CRF_HOME=/Desktop/Workspace/CRF
export CLASSPATH=$CRF_HOME/lib/CRF.jar:$CRF_HOME/lib/colt.jar:$CRF_HOME/lib/LBFGS.jar:$CLASSPATH
I am using linux ubuntu. I am not getting any error but it is just useless. I gave the .sh file permission to execute and I am giving it an order to run or run in terminal. I tried them both, but nothing gives an effect when I want to execute the code.

I tried to modify the file in the same way you did, but still there is not effect. I get the message when I try to execute a command in the terminal

Exception in thread "main" java.lang.NoClassDefFoundError: gnu/trove/TIntHashSet
Let me rephrase my question: how exactly are you executing the code?
It's clearly missing something in its classpath or some similar variable.
Things to try:

echo $CRF_HOME
echo $CLASSPATH

and compare

Other to try:
export CRF_HOME=/Desktop/Workspace/CRF
export CLASSPATH=$CLASSPATH:.:$CRF_HOME/lib/CRF.jar:$CRF_HOME/lib/colt.jar:$CRF_HOME/lib/LBFGS.jar:
@rahmu: I right clicked the file and gave it permission to execute as programs. Then if I double click the .sh, it will give 3 options:
1) Run
2) Run in Terminal
3) Display
I tried options 1 and 2 by clicking them directly from the pop up window.

For the program, first it is built using the ant file. I change my directory to the 'build' folder, and type in the corresponding commands in the terminal.

If I can type in the classpath without using the .sh file, can you please help me with it?

For example, one of the commands is: iitb.segment.Segment train -f sample/us50.conf
I gave the iitb.segment.Segment an alias (CRF) in the .bashrc
So now i call CRF train -f sample/us50.conf
If it is possible to add the classpath to the alias, it would be much better, but is it possible?
@xterm Still not working and the echo is giving me an empty line in the terminal
The problem is that you're not executing the sh script correctly. Do the following in the same terminal session:
$ export CRF_HOME=/Desktop/Workspace/CRF
$ export CLASSPATH=$CRF_HOME/lib/CRF.jar:$CRF_HOME/lib/colt.jar:$CRF_HOME/lib/LBFGS.jar:$CLASSPATH
$ ant build.xml # or whatever your build command is
$ CRF train -f sample/us50.conf
If this works, then you can create a launcher script or a launcher alias (like you suggested). But first make sure the above works.

@xterm: changing the order of entries in the PATH variable isn't going to magically solve the issue and for the love of God, do not include '.' in any sort of PATH. Please ....
rahmu wrote@xterm: changing the order of entries in the PATH variable isn't going to magically solve the issue and for the love of God, do not include '.' in any sort of PATH. Please ....
1) I've had instances previously where the JRE had bugs not capable of finding tools.jar if it wasn't located at the beginning of classpath
2) I'm not stupid enough to include . in the classpath, i'm simply ruling out the possibility of the script running outside the correct path.
Also, and I just realized this, Ubuntu has no such directory. You will definitely get an error.

Try replacing this:
export CRF_HOME=/Desktop/Workspace/CRF
with this:
export CRF_HOME=~/Desktop/Workspace/CRF
Good catch, that's most probably it.
Thank you guys. It is working now. I added the ~ and created an alias.