Bootstrap FreeKB - Java - Import a package class in Eclipse
Java - Import a package class in Eclipse

Updated:   |  Java articles

In Java, the import statement is used to import a class in a package. For example, the following command imports the class named Hello in the package named my.pkg.

import my.pkg.Hello;

public class Main {
	public static void main(String[] args) {
		Hello();
	}
}

 

If the class in the package cannot be located, to the left of the import statement will be a red X.

 

This typically means that the my.pkg.Hello class is in a JAR file, and the JAR file has not been added to your Java Build Path in Eclipse. 

  1. Right-click on your project and select Properties.
  2. Select Java Build Paths.
  3. Select the Libraries tab.
  4. Select Add External JARs.
  5. Select the JAR and select Apply.

In this example. Hello.jar has been added to the Java Build Path.

 


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 com.ibm.ws.webcontainer.servlet.ServletWrapper 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.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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