FreeKB - Eclipse Import a package class
Eclipse - Import a package class

In Java, the import statement is used to import a class in a package. For example, the following command imports the "HelloWorldRemote" class in the "com.ejb.view" package. If the class in the package cannot be located, to the left of the import statement will be a red X.


The packages and classes being imported will be in a JAR file. When there is a red X to the left of the import statement, this usually means that the JAR file that contains the package and class has not been added to the Java Build Path in Eclipse, or the JAR does not contain the class. For example, let's say EJBTest.jar contains the "com.ejb.view" package and the "HelloWorldRemote" class. The JAR can be added to the Java Build Path.


If the package and class are found in the JAR, then red X should no longer be displayed to the left of the import statement, and the class can be used in your Java project.


If the class in the package cannot be located when the app is deployed to the application server, and exception should be caught in the application servers log. Here is an example of WebSphere's SystemOut.log showing that the com.ejb.view.HelloWorldRemote package class could not be found.

00000093 ServletWrappe E service Uncaught service() exception thrown by servlet ejb.client.EJBServlet: java.lang.NoClassDefFoundError: com.ejb.view.HelloWorldRemote
Caused by: java.lang.ClassNotFoundException: com.ejb.view.HelloWorldRemote


When deploying your app to an application server, such as JBoss, Tomcat, or WebSphere, you can either place the JAR in the /WEB-INF/lib folder of your application, or if the JAR is located on the application server, you can use the application servers JAR. If you have numerous apps on your application server that will be using the same JAR, it probably makes sense to not include the JAR in the apps /WEB-INF/lib directory, and to instead configure the application server to be able to provide the app with the JAR. 


Export JAR to /WEB-INF/lib

If you want the JAR to be placed in your apps /WEB-INF/lib folder when the WAR is compiled, you'll want to add a Deployment Assembly. When the WAR is deployed to your application server, the JAR will be in the /WEB-INF/lib directory of your app.

  1. Select Deployment Assembly.
  2. Select Add, then select Java Build Path Entries and click Next.
  3. Select the JAR and select Finish.
  4. Select Apply.

Now, the JAR will contain "Publish/export dependency: /WEB-INF/lib.


Get JAR from Application Server

As an example, following is how to configure a JVM on WebSphere to use a class in a JAR.

  1. In the WebSphere admin console, select Environment > Shared libraries.
  2. Select New.
  3. Give the shared library a name (EJB in this example).
  4. In classpath, enter the full location to the JAR file.
  5. Select OK.
  6. Select Save.


Configure the JVM to use the EJB shared library.

  1. In the WebSphere admin console, expand Servers > Server types > WebSphere application servers.
  2. Select your JVM.
  3. Expand Java and Process Management and select Class loader.
  4. Select New.
  5. Select Classes loaded with parent class loader first and select OK.
  6. Select Save.
  7. Select the newly created class loader.
  8. Select Shared library references.
  9. Select Add.
  10. Select EJB.
  11. Select OK.
  12. Select Save.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 7bc77 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |