0
votes

I created simple Java Servlet: WelcomeServlet.java.

Than, I tried compile this file via:

javac WelcomeServlet.java

In result I see compile error:

package javax.servlet doesn't exit

I try find solution for this error with Google. And I find first part of answer: java compiler doesnt see servlet-api.jar file.

I know, that Apache Tomcat in it lib folder contains servlet-api.jar file.

So, I have this file, but where I must copy this file??

I try different folders:

echo %JAVA_HOME%
C:\Program Files\Java\jdk1.6.0_26

%PATH% contains this line: C:\Program Files\Java\jdk1.6.0_26\bin

So, I copy in:
%JAVA_HOME%\bin
%JAVA_HOME%\lib
%JAVA_HOME%\jre\lib

And in result same error. And only after I copy servlet-api.jar in directory: %JAVA_HOME%\jre\lib\ext

compilation complite sucessful.

My question: Why? Why I must copy in folder %JAVA_HOME%\jre\lib\ext ??

Where This moment describe in documentation?

And other question we have some official docs or specifications that describe folder structure for jdk folder??

2
Must for the puttings of servlet JAR inside classpath.Michael
You should never copy/move existing libraries around. This will only end up in a runtime classloading disaster. You've got to tell the Java compiler where they are by specifying the -cp or -classpath argument which represents the classpath (which is just a collection of disk file system paths pointing to dependency classes and JARs). Please undo all copy/move actions and keep the JRE and Tomcat's /lib folders untouched.BalusC

2 Answers

2
votes

You'll need to specify the directory or directories you want the compiler to search by using the -classpath command line option when running javac. The reason the compiler found your .jar in %JAVA_HOME%\jre\lib\ext is because it searches the extension directories by default.

This is for Java 1.5, but I believe it is more or less still correct:

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/findingclasses.html

2
votes

The link Shaun provides is a more complete answer. But in short, using the classpath is the best way to introduce 3rd party or external (to the JDK/JRE) libraries. The classpath is a concept much like the %PATH% or the $PATH variables, but specifies locations for java to use for lookup rather than the shell to use for lookup of executables.

The classpath provides the java compiler or java virtual machine a list of items to use when searching for resources. This "path" may include directories or files. It will typically include jar files and sometimes locations of configuration files. Many Java based lookup schemes for files configuration or otherwise use some variant of what is accomplished by [Class#getResourceAsStream()][1]'s use of walking the Classpath.

I have rarely seen an incident where putting a jar file in the lib/ext location was preferred to utilizing the Classpath.

The classpath is typically an environment variable (%CLASSPATH% or $CLASSPATH) or specified on the command line when running java or javac (e.g. -cp or -classpath see the help from the executable you are running).

Build tools such as Ant and Maven will also provide abstractions to defining the list of jars to be utilized by your applications and are highly recommended to be used for any length of repetitive change code, build, test, run cycles.