java.lang.NoClassDefFoundError, java.lang.ClassNotFoundException

When you add a dependency reference to a Java project, it may compile without any errors, but runtime you may get an error message similar to this:

HTTP Status 500 – Internal Server Error

Type Exception Report

Message An exception occurred processing JSP page [/index.jsp] at line [1]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/index.jsp] at line [1]

1: <jsp:forward page=”….html” />

Root Cause

javax.servlet.ServletException: Servlet.init() for servlet [spring] threw exception…

Root Cause

java.lang.NoClassDefFoundError: org/springframework/core/env/PropertySource…

Root Cause

java.lang.ClassNotFoundException: org.springframework.core.env.PropertySource…

If the newly added dependency also depends on another package, and that package is already in the pom.xml file, NetBeans will not show an error during build.

If the newly added dependency was recently introduced, and the version of the package already included in the pom.xml file,  references an earlier version, the runtime error will occur.

For example org.springframework.web.bind.annotation.GetMapping was introduced in Spring Web 4.0

If your pom.xml file already contains the reference to spring-web or spring-webmvc version 3, your application will fail runtime.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

In your web browser navigate to the Maven repository at https://mvnrepository.com/artifact/org.springframework and search for the spring-web and spring-webmvc packages. Get the latest stable version numbers and update your pom.xml file.

 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.0.2.RELEASE</version>
 </dependency>

 

 

Java Spring MVC web application configuration in NetBeans 8

When you create a Java Spring MVC web application or make changes to an existing one, it is important to know the location of configuration values.

Java package name

The Java package name is stored in

  • Every .java file
    • package MY_PACKAGE_NAME
  • pom.xml file
    • <configuration>
        <mainClass>com.objectdb.Enhancer</mainClass>
          <arguments>
            <argument>MY_PACKAGE_NAME.MY_MAIN_CLASS_NAME</argument>
          </arguments>
      </configuration>
  • .jsp file
    • <%@page import=”MY_PACKAGE_NAME.*”%>
    • <jsp:useBean id=”MY_BEAN_ID” type=”MY_PACKAGE_NAME.MY_DAO_CLASS_NAME” scope=”request” />

WAR file name

The name of the WAR fle is set in the

  • pom.xml file
    • <finalName>MY_WAR_FILE_NAME</finalName>

Add references to external classes

When you copy a class from another project, you need to add references to the imported external classes. When you build your project, the missing references will be in the output window.

————————————————————-
COMPILATION ERROR :
————————————————————-
webapp/twitter_keywordsRepository.java:[3,42] error: package org.springframework.data.repository does not exist

To add the reference to the project

  1. Click the error message link in the output window,
  2. Click the light bulb in the line of the error,
  3. In the context menu select Search Dependency at Maven Repositories for
  4. Select the recommended Maven repository, and click the Add button.

The references are stored in the

  • pom.xml file under
    • <dependencies>

 

 

 

Start a new Spring MVC Java Web application development in NetBeans

When you create your first Spring MVC Java web application in the base installation of NetBeans follow the steps below

Create a project

  1. In the File menu select New Project
  2. Select the Java Web category, select the Web Application project, and click Next,
  3. Enter the name of the project and click Next,
  4. If this is the first Java web application you create in NetBeans, most likely there is no web server for Java is installed on your workstation. If this is the case, click the Add… button,
  5. Keep the GlassFish Server selected and click Next,
  6. Select the I have read… checkbox and click the Download Now… button,
  7. Keep the latest version selected and click OK,
  8. Make sure the I have read… checkbox is still selected, and click Next,
  9. Click Finish to register the domain on your workstation,
  10. The Context Path shows the address for your application. Click Finish again to continue.
  11. Select the Spring Web MVC framework and click Finish.
  12. If a library is missing from your workstation, a message pops up. Click the Resolve Problems… button for instructions,
  13. On the Resolve Project Problems window click the Resolve… button,
  14. Download the missing library to your workstation. In this case, navigate your web browser to http://www.java2s.com/Code/Jar/j/Downloadjavaeeendorsedapi70jar.htm
  15. Download the “javaee-endorsed-api-7.0.jar” library
  16. Create the folder C:\Users\YOUR_USER_NAME\AppData\Roaming\NetBeans\8.2\modules\ext\javaee7-endorsed
  17. Extract the “javaee-endorsed-api-7.0.jar” file into the new folder,
  18. On the Ant Library Manager window click the New Library… button,
  19. Enter “javaee-endorsed-api-7.0” into the Library Name field and click OK,
  20. Click the Add JAR/Folder… button,
  21. Navigate the to extracted JAR file at “C:\Users\YOUR_USER_NAME\AppData\Roaming\NetBeans\8.2\modules\ext\javaee7-endorsed” and click the Add JAR/Folder button,
  22. On the Ant Library Manager window click the OK button,
  23. On the Resolve Project Problems window click Close.

Start the application

  1. In the Project list right-click the web application project and select Run
  2. The Output window should display a similar message,
  3. Open a web browser and navigate to http://localhost:8080/MY_PROJECT_NAME, to display the index page. The server is case sensitive, so make sure the spelling of the project name is correct.

 

The application configuration files

With the default settings the index.jsp composes the index page. It is in the Web Pages/WEB-INF/jsp directory.

The web.xml file in the Configuration Files directory points to the first Java Server Pages file: redirect.jsp

The redirect.jsp file is located in the Web Pages directory

The Dispatcher

The dispatcher-servlet.xml file contains the mapping between the HTML file, controller, and view.

<property name="mappings">
  <props>
    <prop key="index.htm">indexController</prop>
  </props>
</property>

The bean section specifies the name of the view for the controller.

 <!--
 The index controller.
 -->
 <bean name="indexController"
       class="org.springframework.web.servlet.mvc.ParameterizableViewController"
       p:viewName="index" />

Web Application Build and Deployment

To generate a war file of your web application

  1. In the Project Files directory right click the pom.xml file and select Run Maven > Goals
  2. In the Goals field type
    compile war:war

    and click OK

  3. NetBeans creates the war file in the target directory.

 

Configure NetBeans for Java Web development

The base installation of NetBeans does not include the web development category.

 

To add the Java web development tools to NetBeans

  1. Start the NetBeans IDE,
  2. In the Tools menu select Plugins,
  3. On the Available Plugins tab select the Java EE Base plugin and click the Install button,
  4. If any of the installed plugins is missing the valid signature, you need to click the Continue button to allow the installation,
  5. For the plugin installation, you need to restart the NetBeans IDE,
  6. During the next start, NetBeans installs the new plugins,
  7. The Java Web category is now available.