You need
to access code in a
.jar
or .class
file in your
project, but Eclipse can’t find these files.
Select the project in the Package Explorer, and then select
Project→ Properties to open the Properties dialog. Click the
Libraries tab in this dialog, click Add External JARs for
.jar
files or Add Class Folder for
.class
files, navigate to the
.jar
file or to the folder containing
.class
files, and click OK.
Often you need other code in the build path, such as
.class
or .jar
files. For
instance, say you’re developing a Java servlet, as
shown in Example 4-3.
Example 4-3. A simple servlet
package org.cookbook.ch04; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter( ); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>"); out.println("Using Servlets"); out.println("</TITLE>"); out.println("</HEAD>"); out.println("Using Servlets"); out.println("</BODY>"); out.println("</HTML>"); } }
A lot of the support for servlets is in
servlet.jar
. Eclipse can’t find
servlet.jar
by itself, so a lot of wavy red
lines will appear when it comes to the imports, as shown as in Figure 4-17.
To add servlet.jar
to the build path, select
Project→ Properties, and click the Libraries tab. Then click
Add External JARs, navigate to servlet.jar
, and
click OK. Doing so adds servlet.jar
to the build
path, as shown in Figure 4-18. Click OK to close the
Properties dialog, and then build the project; when you do, things
will work out fine (and you’ll see
servlet.jar
in the Package Explorer).
If you add multiple .jar
files to the classpath,
you also can indicate the order in which you want them searched. Just
click the Order and Export tab in the Properties dialog, and change
the order of imported items by using the Up and Down buttons.
Tip
If you know you’re going to be using a
.jar
file such as
servlet.jar
when you first create the project,
you can add that .jar
file to the
project’s classpath in the third pane of the New
Project dialog. You’ll see the same tabs there as
you do in Figure 4-18. Just click the Libraries tab,
and add the .jar
files you want to the project.
If you know you’re going to be using a
.jar
file such as
servlet.jar
often, you might want to create a
classpath variable
. Doing so will save you time
when you want to include items in a project’s build
path. Using classpath variables like this is not only convenient, but
also it centralizes your classpath references for easy handling. For
example, if you want to use a new version of
servlet.jar
across multiple projects, all you
have to do is to update one classpath variable.
To create a classpath variable, select Window→
Preferences→ Java→ Classpath Variables, as shown in
Figure 4-19. Click New, enter the new
variable’s name—we’ll use
SERVLET_JAR
here—enter its path (or browse
to its location), and then click OK. You can see this new variable in
the figure.
When you want to add this classpath variable to a project’s classpath, open the project’s Properties dialog, click the Libraries tab, click the Add Variable button (shown in Figure 4-18), and select the variable you want to add to the classpath.
Recipe 1.5 on creating a Java project.
Get Eclipse Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.