1

I'm very new to the Java landscape (Eclipse, Spring, etc.) and just walking through some introductory tutorials at the moment. I just finished a Hello World tutorial for Spring MVC, with a simple page that then posts to another action. Straightforward enough.

I then went on to tinker a bit with the working result of that tutorial. My first attempt is to create another project in Eclipse with just some simple packages containing POJOs. The idea being to have something portable which different applications could use. (Intended to be the equivalent of a "Class Library" project in the .NET world.)

That project has one package, with one interface (IWorld) and one class (HelloWorld). In my Spring MVC project I then add the other project to the build path and create an instance of that object in the controller. The compiler says everything's fine:

package com.sandbox.application.mvctest.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sandbox.domain.models.*;

@Controller
public class HelloWorldController {

    @RequestMapping("/")
    public String hello() {
        IWorld world = new HelloWorld();
        world.speak();
        return "hello";
        //TODO: Return a ModelAndView instead
    }

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String hi(@RequestParam("name") String name, Model model) {
        String message = "Hi " + name + "!";
        model.addAttribute("message", message);
        return "hi";
    }

}

The .speak() method doesn't do anything of note, just returns a value which the controller isn't using yet. The intent of course is to start building up some features in the models rather than just in the controllers and views, building more of a rich domain model.

Eclipse doesn't complain about the code, everything seems to compile just fine. But when executing on Tomcat I get an exception:

Jul 05, 2017 12:36:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet [springDispatcher]
java.lang.ClassNotFoundException: com.sandbox.domain.models.IWorld
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    [and so on...]

My Googling has proved fruitless so far, likely because I don't always understand what I'm finding. For example, there are some posts which advise to correct something in a pom.xml file. I have no such file. Is this related to Spring's dependency injection? Should I be using that? I wouldn't have considered this to be an injectable dependency, I'm just creating an instance directly. But then I'm also approaching this from a deeply dogmatic .NET mindset, so all bets are off.

What am I missing here?

David
  • 208,112
  • 36
  • 198
  • 279
  • 3
    You need to package your "Class Library" and include it in your spring application. In Java, that is typically a [jar file](https://docs.oracle.com/javase/tutorial/deployment/jar/). Normally we would use [`maven`](https://spring.io/guides/gs/maven/) for packaging the libraries (it's a project comprehension / dependency management tool). And it is `maven` that uses `pom.xml`. The `maven` plugin for eclipse is called m2e. And you will probably want to ["mavenize"](https://stackoverflow.com/questions/2449461/convert-existing-eclipse-project-to-maven-project) your project. – Elliott Frisch Jul 05 '17 at 16:46
  • @ElliottFrisch: Is there something I can manually do in Eclipse to include it? I'm looking at the Properties for the MVC project, and I see the other project selected under "Project References" as well as listed under Projects in "Java Build Path". At the very least it sounds like I should consult a Maven tutorial earlier than I'd expected to as well. (Edit: Just saw your edit. That mavenize link looks very helpful, thanks!) – David Jul 05 '17 at 17:04

2 Answers2

1

If I understand your question properly then you are expecting the project class-path and the web app class-path to be the same. All the dependencies would not be exported automatically when the web app is deployed. You can check the deployed web-app's WEB-INF/lib folder for your dependencies.

If you are using Tomcat as your servlet container then you can add the dependency jars in Tomcat's lib folder and then it should work.

Zeeshan Adnan
  • 769
  • 6
  • 10
  • This got me up and running. Specifically (and now I am recalling this from a similar endeavor in the past) I needed to add the "Class Library" project to the "Deployment Assembly" section of the project's Properties. Thanks! – David Jul 05 '17 at 17:14
-2

you are using springframework, so you can create the bean of the com.sandbox.domain.models.IWorld and use the @Autowired tp Dependency Injection. refer code

@Autowired
private IWorld world;
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17