I think Java tends to be frustrating for newbies (and sometimes veterans) alike because of Java's CLASSPATH variable. I'm going to be minimalistic and say that the CLASSPATH is essentially a search path for where the *.class files are kept when you compile or execute a java program. When the compiler or JRE is invoked, one of the things it does is locate packages, objects, *.class files, and *.jar files (or more). If it can't find it, the compiler/JRE bombs with an error. But the solution is actually quite easy: just add all your objects to the CLASSPATH environmental variable! What does that mean? Rather than go on, I'll show you what I do: Under my shell environment start up scripts (like .cshrc, .bashrc, etc.), I add all my libraries here so Java (and other like minded interpretters/compilers) won't complain. For instance, let's say I want to use postgres, ant and XML. Here's how I will set up file: export PG_HOME=/opt/databases/pgsql export JAVA_BASE=/opt/java export XML_JAVA=$JAVA_BASE/xml export ANT_HOME=$JAVA_BASE/jakarta/ant export JAVA_HOME=$JAVA_BASE/jdk export XERCES_HOME=$XML_JAVA/xerces my classpath will then include the jar files and possible classes (if any): export CLASSPATH=${ANT_HOME}/lib/ant.jar: ${XERCES_HOME}/xercesImpl.jar: ${XERCES_HOME}/xmlParserAPIs.jar: ${PG_HOME}/share/java/postgresql.jar: When i run javac or java on a program that uses these libraries, I won't get the errors associated with the CLASSPATH issue. Actually, you should use some build utility like ant which does a better job of managing this problem. That will be discussed in a different article. But the basic idea here is that CLASSPATH isn't a difficult issue once you understand what it represents.
Trackbacks: (Trackback URL)
