Understanding class paths in Java EE projects with Rational Application Developer Version 8.0 by Neeraj Agrawal, IBM This article describes a variety of class path scenarios for Java™ EE 1.4 projects and Java EE 5.0 and above projects using IBM ® Rational ®Application Developer Version 8.0. From a runtime perspective, in Java EE modules until version 1.4 : a. The modules of an EAR file access other modules by specifying an entry in the MANIFEST.MF file. b. Web modules also have access to JAR files inside its \WEB-INF\lib folder. In Java EE 5.0 and above modules, the JAR files in the lib folder of an EAR file are also available in the class path of the modules without an explicit MANIFEST.MF entry. Common classpath scenarios Let us understand how to configure the class path in the module projects of a sample EAR file : Figure 1: Java EE 1.4 projects If you want to access EJBs which are in the EAR14EJB project from the EAR14Web project and your previous experience is with the Java development tools in Eclipse, you might attempt to add a reference to the EAR14EJB project. Page 1 of 9 Figure 2: Web project Java Build Path This approach might appear to work well in Rational Application Developer since you do not see any compilation errors. However, as soon as you deploy the application to WebSphere® Application Server, you will start seeing ClassNotFound exceptions. The reason for these exceptions is that you did not put an entry in the manifest of the Web project. The correct way of configuring the class path is to open the deployment assembly page of the Web project. Right-click your Web project and select Properties > Deployment Assembly. On the Deployment Assembly page, add a reference to the EJB project as shown below on the Manifest Entries tab. Page 2 of 9 Figure 3: Deployment Assembly Page Once you add a reference, you will notice that the EAR Libraries are updated with the EJB project. Figure 4: EAR Libraries container in the Web Project Page 3 of 9 EAR Libraries: is a container which provides the JAR file from your manifest into the project class path so that you do not need to perform an additional step to configure the Java Build path yourself as covered in Figure 2. The User library scenario is covered later which automatically populates EAR Libraries. Web App Libraries: If you pay close attention in Figure 4 you will notice that there is an empty folder called Web App Libraries. This folder is also a container which provides JAR files placed in the WEB-INF\lib folder into the project class path of the Web project. There are two common ways to put a JAR file inside WEB-INF\lib: a. By physically dropping inside the folder which is typically found under \WebContent\WEB-INF\lib or b. Logically mapping a JAR file to \WEB-INF\lib which is achieved using the deployment assembly properties page (refer to Figure 3). On the deployment assembly properties page, click the Deployment Assembly tab and select Add. Figure 5: Deployment assembly reference wizard You can choose Archive, External Archive and Variable to configure the JAR file. This JAR file is deployed properly on WebSphere Application Server when you choose to run your EAR file, likewise, if you export the EAR file you will see the JAR file being bundled in the archive. Page 4 of 9 In the example below, I have added log4j.jar which is placed somewhere in my disk. You will notice that the Web App Libraries container is updated. Figure 6: External JAR file configured in a Web project You can also make another project in your workspace available to the class path by simply selecting the Project option in Figure 5 and selecting an available project there. User Library Support for Web Projects Consider the case where you have a number of JAR files residing in a folder in your disk and you want to use them in your Web project. One approach would be to add each JAR file one by one as discussed under the Web App Libraries section. However, this approach is not only time consuming but not ideal for working in a team environment. Fortunately, there is a better way to accomplish this task by using User Library support. To use this feature, select Classpath Container as displayed in Figure 5 which opens the Add Library wizard. Page 5 of 9 Figure 7: Add Library Clicking Next opens another wizard page which lets you select User Libraries. The User Library page is also found on the Java >Build Path > User Libraries preference page. Page 6 of 9 Figure 8: User Library Page 7 of 9 Figure 9: Web Project after adding User Library As mentioned earlier in respect to external archives, user libraries are also logically mapped to \WEB-INF\lib and are deployed properly on WebSphere Application Server. User Library Support for EAR 5.0/6.0 Projects In EAR 5.0/6.0 you can use a similar mechanism as discussed under “User Library Support for Web Projects” to configure user defined JAR files. For an EAR file, it is mapped by default to \lib and all the modules of the EAR have visibility of those jars in their class path by means of EAR Libraries. In the example below I have selected a previously created user library in a 6.0 EAR file. The module of EAR, EAR60EJB automatically finds all the JAR files in its EAR Libraries container. Figure 10: EJB 3.1 project with EAR user libraries configured Page 8 of 9 Neeraj Agrawal architects and develops Java EE tools for Rational Application Developer. He is also a contributor to the Eclipse Web Tools Platform. He can be reached at nagrawal@us.ibm.com © Copyright IBM Corporation 2010. Page 9 of 9