0

I have an existing Java Project that runs fine by itself.

I created a new Dynamic Web Project and am using Tomcat to run the Dynamic Web Project. In Eclipse I selected the build path of the Dynamic Web Project and went to Java Build Path->Projects and selected the existing Java Project.

In my servlet I can import myPackage.myClass; from the existing Java Project and run the servlet without issue. Yet if I use anything in that class, even a simple myClass testclass = new myClass(); I get a ClassNotFound exception.

Do I need to do anything else to make this servlet call and use this other package? Both projects are in the exact same directory.

Dir structure looks like this:

parentDir/ myPackage/ myServlet/

I tried adding the Class Folder by going to Java Build Path->Libraries->Add Class Folder... and selecting the project folder as well, but I get an error in the dialog box saying Build path contains duplicate entry:

I've never worked with servlets before nor have I used multiple packages, so I'm unsure what I'm doing wrong or if this can even work.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
WildBill
  • 9,143
  • 15
  • 63
  • 87
  • Are you putting the jar inside `WEB-INF/lib` of your `war` file which has that servlet? for better manageability you can use maven build – sanbhat Apr 14 '14 at 08:12
  • have you tried a clean and build? – MaVRoSCy Apr 14 '14 at 08:12
  • hey where is your said package is it in src or existing project or is it some other jar which you are trying to incorporate into this if yes add it lib in WEB-INF/lib and for safety just add it also to build – codefreaK Apr 14 '14 at 08:13
  • @sanbhat - jar file for myPackage? or the servlet? I typically just run the code for myPackage through Eclipse. Do I need to create a jar file for the servlet to use? – WildBill Apr 14 '14 at 08:14
  • @user3127499 - both the servlet and the package are in the exact same directory. – WildBill Apr 14 '14 at 08:14
  • @MaVRoSCy - I have cleaned the project – WildBill Apr 14 '14 at 08:16
  • If your `MyClass` is in a `jar`, then add that `jar` to `WEB-INF/lib`, otherwise make sure `MyClass` is within `WEB-INF/classes`. Usually copying to desired location is done by the build management tools such as Ant/Maven. – sanbhat Apr 14 '14 at 08:17
  • Can you please add the screenshot of your eclispe src folder – codefreaK Apr 14 '14 at 08:19
  • @sanbhat - importing the jar file worked! Thanks a ton. The removal of my hair can now cease... – WildBill Apr 14 '14 at 08:30
  • @sanbhat - if you make this a reply (and not a comment) I will accept it as an answer. – WildBill Apr 14 '14 at 13:12

1 Answers1

-1

You can include the package in to servlet like below

<%@page import="java.util.*"%>

It will then looks like,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@page import="java.util.*"%>