1

I am using ubuntu and I have set my paths to be the following:

JAVA_HOME=/usr/local/jdk1.6.0_24
export CLASSPATH=/usr/local/tomcat/lib
export JAVA_HOME

I thought that would put the servlet libraries in the compile path, but I am still getting compile errors like this:

package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;

Any ideas how to fix this or what I am doing wrong? The general Java libraries seem to be working fine.

Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • 1
    I've answered that before: http://stackoverflow.com/questions/5600543/java-compilation-gives-errors-about-servlet-libraries/5605416#5605416 Did you notice it? – BalusC Apr 11 '11 at 18:21

1 Answers1

7

With jar files, simply specifying a directory containing jar files will not work. You have two options:

  1. Specify each jar file individually on the CLASSPATH:

    export CLASSPATH=/usr/local/tomcat/lib/servlet-impl.jar:/path/to/another.jar
    
  2. Since you're using Java 6, you should be able to use wildcards (to include all jars in a directory):

    export CLASSPATH=/usr/local/tomcat/lib/*
    
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • +1. Also, without wildcards, you can always do something like `export CLASSPATH=&backquot;echo DIR/*.jar | sed 's/ /:/g'&backquot;` – khachik Apr 11 '11 at 18:23
  • @Rob This is somehow still not working. Does it require a reboot usually? – Genadinik Apr 11 '11 at 18:23
  • @khachkik - Indeed, and that's been a common idiom that I've seen done in pre-5 environments. – Rob Hruska Apr 11 '11 at 18:23
  • @Genadinik - It shouldn't require a restart of any kind, unless you're working in some strange environment that you've not divulged. Did you try using #1 or #2? – Rob Hruska Apr 11 '11 at 18:25
  • @Rob even for compiling, I still need to let the classpath know about those tomcat libraries, correct? What could be going wrong? – Genadinik Apr 11 '11 at 18:28
  • @Genadinik - Correct, they need to be on the CLASSPATH at both compile-time and runtime. Perhaps try option #1 and see if it works for you. – Rob Hruska Apr 11 '11 at 18:35
  • @Rob by the way, I just tried downloading servlet.jar and placing it in the /usr/local/jdk1.6.0_24/jre/lib/ext directory. Should that kind of thing work? – Genadinik Apr 11 '11 at 18:40
  • @Rob just tried this: CATALINA_HOME=/usr/local/tomcat JAVA_HOME=/usr/local/jdk1.6.0_24 export CLASSPATH=/usr/local/tomcat/lib/*:/usr/local/jdk1.6.0_24/jre/lib/ext/servlet.jar export JAVA_HOME but it didn't work – Genadinik Apr 11 '11 at 18:45
  • @Genadinik - Regarding downloading the servlet.jar, it *might* work, but probably not the way you need it to. Is there a `servlet-api.jar` in your `tomcat/lib` directory? If so, what happens when you do `CLASSPATH=/usr/local/tomcat/lib/servlet-api.jar`? – Rob Hruska Apr 11 '11 at 18:49
  • @Rob I just tried to do that command and it still gave compile errors. There is that .jar in the tomcat/lib. By the way, I just tried another thing. Something like this: sudo update-alternatives --config javac .....and it gave me choices of a diff directory for my java paths. It asked me about this: /usr/lib/jvm/java-6-sun/bin/javac ....where can that be set? It might be overwriting some of the configs I am trying to set. – Genadinik Apr 11 '11 at 18:53
  • @Genadinik - I wouldn't think the alternatives configuration would be overriding the classpath you're setting. Alternatives is mainly to control which versions of java/javac/etc. are made available on the PATH. However, maybe your error does lie within your java installation. There was another answer on here but it got deleted, it may have had some valuable information about that. From where/how did you install the JDK? – Rob Hruska Apr 11 '11 at 19:14
  • @Rob Actually, I just tried to compile command-line and it worked fine. The problem only happens when I compile with ant. Would you know why that would happen? – Genadinik Apr 11 '11 at 19:20
  • @Genadinik - Looks like your [other question](http://stackoverflow.com/questions/5626317/ant-javac-and-commandline-javac-give-different-results) will probably lead you to an answer for that. – Rob Hruska Apr 11 '11 at 19:23