10

I have a Java Project, for which I'm now creating a Web interface, using a Dynamic Web Project from Eclipse. The Web project consists of a single servlet and two JSP's. Something like this:

/JavaApplication
  /src
  /lib
  /resources
/WebApplication
  /src
    /Servlet.java
  /WebContent
    /WEB-INF
    index.jsp
    other.jsp

Now, I need to reference JavaApplication from WebApplication, in order to use its classes to process web requests. What's the best way to accomplish this ? My idea is to create a .jar of the JavaApplication, containing all the .class files, /resources, and /libs. In this way, I could include the .jar in the web application, and I could have a single .war file that contained the entire application.

What do you think? How is this problem typically solved ?

Note: I don't want to convert the Java Project into a Web project.

mjn
  • 36,362
  • 28
  • 176
  • 378
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • I had the same issue. An answer in: http://stackoverflow.com/questions/1693322/eclipse-web-project-dependencies solved it. – Bijan Nov 24 '12 at 23:26

5 Answers5

16

In Eclipse project properties, add the project to the Java EE Module Dependencies (Eclipse 3.5 or older)

Java EE Module Dependencies

or Deployment Assembly (Eclipse 3.6 or newer) entry in the project properties.

Deployment Assembly

This way Eclipse will take care about doing the right thing to create a WAR out of this all (it will end in /WEB-INF/lib). No other configuration is necessary, even not some fiddling in Build Path.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
15

Under Eclipse, you can declare Project References for a given project, the web application in your case. To do so, right click on your web application project, then go for Properties > Project References and select the JavaApplication project. This should allow you to call code from the JavaApplication project from the WebApplication without having to build a WAR. This is a solution for development.

For standard deployment (outside the IDE), you should indeed create a standard WAR. To do so, you'll have to package your JavaApplication as a JAR including the .class files and the files under /resources but not the libraries it depends on (JARs under /lib). These dependencies will actually end up in the WEB-INF/lib directory of the WAR, beside the JAR of your JavaApplication. These steps are typically automated with tools like Ant or Maven.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Yes, I'm using that solution for development. As for deployment, that's what I'm probably going to do, after researching a bit the .EAR files. Thanks. – João Silva Nov 07 '09 at 14:42
  • 2
    Did this suggestion work, I am trying it, but keep getting java.lang.NoClassDefFoundError at runtime although everything compiles correctly in my servlets. – myahya Nov 24 '10 at 15:17
4

Connecting java app to web app for development :

right click on web project :

properties>project references> add the java project you want to refer

Now in properties tab of web project go to

properties>deployment assembly> add the project manually and run the app

Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
Heether
  • 152
  • 1
  • 1
  • 6
2

Consider moving up to EAR level, if your web container supports that.

The tricky part with shared code is where should the common code be put. A copy pr web application? A copy in the web container? Overdoing the "share these classes" might end up in class loader problems.

If you are creating two separate web applications refactor common java code into a separate Eclipse project and refer to it from both WAR projects.


EDIT: Apparently I have misread the problem description, and thought you asked about an existing and a new web application sharing code.

If you have an Eclipse project with your application, and another with your web frontend, then you can let your application export the necessary resources which the "Export WAR" in Eclipse Java EE can wrap up in a jar file and put in WEB-INF/lib for you. You need to say this explicitly with a checkmark in Properties -> Java EE Module Dependencies for your web project. Expect you have to experiment a bit - this took me a while to learn.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Thanks, I'll look into the EAR solution. With respect to shared code, there isn't any. The web application is a simple front-end to the java application, which makes use of an `Engine` to process requests and feed some simple beans to the JSPs. Since this interface is for demonstration purposes, it needs to be easily deployed (I'm currently using `jetty-runner`). For development, I'm using the solution @Pascal described, but I'm still looking for a better solution for deployment (currently, I'm passing everything I need [libs, resources, .classes] to `jetty-runner`). – João Silva Nov 07 '09 at 14:48
0

Typically you would create an API interface using remote service beans from the Java application that expose the methods that you want to invoke in the web application. You would include a proxy of the API interface with your web application that calls the remote service bean in the Java application. Remember that you will need to register the remote bean in the web.xml file.

Antz
  • 281
  • 1
  • 5