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>

 

 

Only persistence units with transaction type JTA can be used as a container managed entity manager

NetBeans can use multiple deployment servers. When your web application uses the Entity Framework and you select the Glassfish server as the deployment server, you can get the error message when you try to run your application in the IDEÉ

Severe: Exception while preparing the app : The persistence-context-ref-name […] in module […] resolves to a persistence unit called […] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.

When you first run the application NetBeans asks you to select the deployment server you want to use.

If only the Glassfish server is available, and Tomcat is installed on your workstation, add Tomcat to the available deployment servers.

Install Tomcat

For the Tomcat installation see Tomcat web server installation

Add Tomcat to the NetBeans server list

  1. In the Tools menu select Servers
  2. Click the Add Server button
  3. In the list select the Apache Tomcat or TomEEE server
  4. Click the Browse… button
  5. Select the Tomcat directory on your workstation
  6. Enter a Tomcat username and password for NetBeans, and click the Finish button.
  7. The user will be created in the tomcat-users.xml file in the Tomcat conf directory
  8. Click the Close button.

Select the Deployment Server

  1. Right-click the NetBeans project and select Run
  2. In the Select deployment server window select Apache Tomcat or TomEE

If the Select deployment server window does not pop up

  1. Right-click the NetBeans project and select Set Configuration -> Customize
  2. On the Run tab select the Apache server.

Starting of Tomcat failed, the server port 8080 is already in use

  1. If the Glassfish server already started, Tomcat cannot use the same ports and an error message will appear in the NetBeans RunOutput tab
  2. To stop the Glassfish server, restart your workstation.

Create a WAR file for a Java web application in NetBeans

Configure the web application

Add the web.xml file

The wb.xml file contains the web application configuration values for the webserver. I has to be in the WEB-INF directory of the web applicaion. To add the web.xml file to your NetBeans Java web application

  1. Right-click the Netbeans project, and select New -> Other
  2. In the New File window select XML -> XML Document, and click the Next button,
  3. In the New XML Document window enter web.xml as the name of the file, and click the Browse… button to select the location,
  4. Select the src\main\webapp\WEB-INF folder, and click the Select Folder button,
  5. In the New XML Document window click the Next button,
  6. In the New File window keep the Well-formed Document radio button selected and click the Finish button,
  7. NetBeans creates the XML file in the Web PAges WEB-INF folder on the web project.

Build the WAR file

To create a WAR file for a Java web application in NetBeans 8.2

  1. In the NetBeans IDE select the Files tab,
  2. Right-click the pom.xml file and select Run Maven > Goals…
  3. In the Run Maven window enter compile war:war into the Goals field,
  4. Select the Remember as checkbox and enter Compile war for the name,
  5. The WAR file is generated in the target directory of the Java web project.
  6. The next time you want to build the WAR file, the Compile war option will be visible in the Run Maven menu.

Package dependent Java JAR files into the project JAR file

When you execute your Java JAR file, you need to include all dependent JAR files in the “fat jar” file, so the Java virtual machine can find them at runtime.

Set up NetBeans to include the JAR files from the library

In NetBeans you need to add the list of dependent libraries to the build.xml file.

  1. On the Files tab double-click the build.xml file to open it in the editor,
  2. Add the lines below to the build.xml file before the </project> statement. Change the value of the second line to specify the name of your project.
    <target name="package-for-store" depends="jar">
        <property name="store.jar.name" value="MY_PROJECT"/>
        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>
        <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>
            <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
        <zip destfile="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>
        <delete file="${store.dir}/temp_final.jar"/>
    </target>
  3. Save the build.xml file.

Build the project

  1. Right click the build.xml file,
  2. Select Run Target > Other Targets > package-for-store
  3. The generated file is going to be located in the store directory.