DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 4.11 Using .jar and .class Files

4.11.1 Problem

You need to access code in a .jar or .class file in your project, but Eclipse can't find these files.

4.11.2 Solution

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.

4.11.3 Discussion

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.

Figure 4-17. Missing the servlet.jar file
figs/ecb_0417.gif


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).

Figure 4-18. servlet.jar in the build path
figs/ecb_0418.gif


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.

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.


4.11.3.1 Creating classpath variables

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.

Figure 4-19. Creating a classpath variable
figs/ecb_0419.gif


4.11.3.2 Using classpath variables

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.

4.11.4 See Also

Recipe 1.5 on creating a Java project.

    Previous Section  < Day Day Up >  Next Section