![]() |
< Day Day Up > |
![]() |
Recipe 4.11 Using .jar and .class Files4.11.1 ProblemYou need to access code in a .jar or .class file in your project, but Eclipse can't find these files. 4.11.2 SolutionSelect the project in the Package Explorer, and then select
Project 4.11.3 DiscussionOften 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 servletpackage 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![]() To add servlet.jar to the build path, select
Project Figure 4-18. servlet.jar in the build path![]() 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.
4.11.3.1 Creating classpath variablesIf 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 Figure 4-19. Creating a classpath variable![]() 4.11.3.2 Using classpath variablesWhen 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 AlsoRecipe 1.5 on creating a Java project. |
![]() |
< Day Day Up > |
![]() |