1

I followed successfully the excellent tutorial from Oracle showing how to create an embedded tomcat application which I could start via java -jar \targeg\"myExecutableJar". In such tutorial there isn't Spring at all. (Oracle Tutorial

Then I edited the index.jsp as showed bellow. Obviously, I added in pom.xml all Spring lib necessary.

After this, I am getting the error from my question subject. I have read several question with similar point none of them fixed my issue. FOr instance, in certain question someone told to check ecj so I added it in pom. Other answer recommend to use the most recent Tomcat available. I did it.

It seems to me that there might exist some conflict probably between some embedded tomcat lib and other one but I can't imagine which one.

Other possibility might be related to some issue with the spring context especially because it is an embedded tomcat but I can't guess anything else to try.

Am I missing any extra configuration? Kindly, note in the image below that I leave the context.xml under META-INF exactly as automatically generated and I place all the beans definition in App-servlet.xml.

Project Structure and the main method

index.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>

<body>



<h2>Uploading a file.. </h2>

      <form:form method="POST" action="uploadFile" enctype="multipart/form-data">

              Upload your file please:

              <input type="file" name="file" />

              <input type="text" name="name">

              <input type="submit" value="Upload" />

              <form:errors path="file" cssStyle="color: #ff0000;" />

       </form:form>



</body>

</html>

App-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="

   http://www.springframework.org/schema/beans    

   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-4.2.xsd">



       <context:component-scan base-package="com.example.employees" />



       <bean

              class="org.springframework.web.servlet.view.InternalResourceViewResolver">

              <property name="prefix" value="/WEB-INF/" />

              <property name="suffix" value=".jsp" />

       </bean>



       <bean id="multipartResolver"

              class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />



</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">



       <display-name>Spring MVC Application</display-name>









       <servlet>

              <servlet-name>App</servlet-name>

              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

              <init-param>

                     <param-name>contextConfigLocation</param-name>

                     <param-value>/WEB-INF/App-servlet.xml</param-value>

              </init-param>

              <load-on-startup>1</load-on-startup>

       </servlet>



       <servlet-mapping>

              <servlet-name>App</servlet-name>

              <url-pattern>/</url-pattern>

       </servlet-mapping>

       <welcome-file-list>

              <welcome-file>index.jsp</welcome-file>

       </welcome-file-list>

</web-app>

pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

       <modelVersion>4.0.0</modelVersion>

       <groupId>com.example.employees</groupId>

       <artifactId>employees-app</artifactId>

       <packaging>war</packaging>

       <version>1.0-SNAPSHOT</version>

       <name>employees-app Maven Webapp</name>

       <url>http://maven.apache.org</url>

       <properties>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

              <tomcat.version>8.0.32</tomcat.version>

              <spring.version>4.2.5.RELEASE</spring.version>

       </properties>

       <dependencies>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-web</artifactId>

                     <version>${spring.version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>${spring.version}</version>

              </dependency>



              <dependency>

                     <groupId>commons-fileupload</groupId>

                     <artifactId>commons-fileupload</artifactId>

                     <version>1.3.1</version>

              </dependency>



              <dependency>

                     <groupId>org.apache.tomcat.embed</groupId>

                     <artifactId>tomcat-embed-core</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat.embed</groupId>

                     <artifactId>tomcat-embed-logging-juli</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat.embed</groupId>

                     <artifactId>tomcat-embed-jasper</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-jasper</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-jasper-el</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-jsp-api</artifactId>

                     <version>${tomcat.version}</version>

              </dependency>

              <dependency>

                     <groupId>jstl</groupId>

                     <artifactId>jstl</artifactId>

                     <version>1.2</version>

              </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>javax.servlet-api</artifactId>

                     <version>3.0.1</version>

              </dependency>

              <dependency>

                     <groupId>org.eclipse.jdt.core.compiler</groupId>

                     <artifactId>ecj</artifactId>

                     <version>4.5.1</version>

              </dependency>



       </dependencies>

       <build>

              <finalName>employees-app</finalName>

              <resources>

                     <resource>

                           <directory>src/main/webapp</directory>

                           <targetPath>META-INF/resources</targetPath>

                     </resource>

              </resources>

              <plugins>

                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-compiler-plugin</artifactId>

                           <version>3.5.1</version>

                           <inherited>true</inherited>

                           <configuration>

                                  <source>1.8</source>

                                  <target>1.8</target>

                           </configuration>



                     </plugin>

                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-assembly-plugin</artifactId>

                           <configuration>

                                  <descriptorRefs>

                                         <descriptorRef>jar-with-dependencies</descriptorRef>

                                  </descriptorRefs>

                                  <finalName>employees-app-${project.version}</finalName>

                                  <archive>

                                         <manifest>

                                                <mainClass>com.example.employees.Main</mainClass>

                                         </manifest>

                                  </archive>

                           </configuration>

                           <executions>

                                  <execution>

                                         <phase>package</phase>

                                         <goals>

                                                <goal>single</goal>

                                         </goals>

                                  </execution>

                           </executions>

                     </plugin>

              </plugins>

       </build>

</project>

log:

type Exception report

message Unable to compile class for JSP

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

        org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:600)

        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)

        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException

        org.apache.jasper.JspCompilationContext.getTldResourcePath(JspCompilationContext.java:551)

        org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:420)

        org.apache.jasper.compiler.Parser.parseDirective(Parser.java:479)

        org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)

        org.apache.jasper.compiler.Parser.parse(Parser.java:139)

        org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)

        org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)

        org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)

        org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)

        org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)

        org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)

        org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)

        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)

        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

************ Added

enter image description here

Jim C
  • 3,957
  • 25
  • 85
  • 162

2 Answers2

4

This might be due to the servlet.jar jar conflict. Tomcat already comes in with the servlet api library. Try to add the <scope>provided</provide> for the servlet dependency in your pom.xml.

<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <version>3.0.1</version>
     <scope>provided</scope>
</dependency>

This dependency is already provided by your container(ie. Tomcat) so you don't have to include this in your deployed application again.

Other things you can try:

  • Try Project -> Clean and re-run the project
  • You should require at least JSP 2.1 and mark it as provided in your POM

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    
  • One possible cause is if a temp file cannot be written to the current directory.

  • And this article specific for 1.8 version of JDK.
  • In Tomcat 8, Jasper initialization is implemented as a standard ServletContainderInitializer (check Servlet specification). So you should ensure that org.apache.jasper.servlet.JasperInitializer.onStartup is invoked during web app startup. Source

  • Setting the context with, context.addServletContainerInitializer(new JasperInitializer(), null);
  • Related others https://github.com/spring-projects/spring-boot/issues/962 & https://github.com/bmuschko/gradle-tomcat-plugin/issues/132
Lucky
  • 16,787
  • 19
  • 117
  • 151
  • Luck, I added a picture showing the outcome after your suggestion. Kindly, can you see if there is any other conflict? – Jim C Mar 09 '16 at 12:49
  • @JimC Can you do the same for `jstl-1.2.jar` depedency too? – Lucky Mar 09 '16 at 14:09
  • @JimC Updated my answer with other possible solutions. Good luck – Lucky Mar 09 '16 at 15:10
  • Lucky, I read all your suggestions carefully. In my case, it is an embedded tomcat. Do you think I should change tomcat-jsp-api to other jsp-api? Other doubt, please look at the way I am starting the application (main method). How can I add your suggestion "context.addServletContainerInitializer(new JasperInitializer(), null);" there? PS. I have no issue at all in running inside of Eclipse or deploying it as war file to Tomcat. The problem is really something related to Embedded Tomcat (maybe the main method, the folder structure, some lib) – Jim C Mar 09 '16 at 16:09
  • @JimC Can you use the same versions of all the dependencies mentioned in the pom.xml in the example you provided? http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/files/pom.xml You are using tomcat 8 while the tutorial link you followed uses tomcat 7. And do you really want to include Spring jars? Here's the maven project zip file you can download and run from the tutorial link Resources tab http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html#section9 – Lucky Mar 10 '16 at 08:59
  • http://stackoverflow.com/questions/15988578/specify-a-custom-web-xml-to-an-embedded-tomcat for adding context to the embedded tomcat startup code. – Lucky Mar 10 '16 at 09:13
  • The example works perfectly but have a note that it doesn't provide a Controller or MVC functionality. I want to move some rest web services to Embedded Server to work with Micro Service ideas. The reason to add Spring will be clear if you look to the Controller I pasted above. Well, if Spring is the issue, I can start take other approach (actually I already start to look to Jersey alternative. See the last comment in http://blog.sortedset.com/embedded-tomcat-jersey/ Yesterday) but I faced similar issues (it runs in Eclipse and installed Tomcat but I can't start as executable jar in console) – Jim C Mar 10 '16 at 14:41
1

remove following dependency from pom.xml . add manually project run time .

          <dependency>

                 <groupId>org.apache.tomcat.embed</groupId>

                 <artifactId>tomcat-embed-core</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat.embed</groupId>

                 <artifactId>tomcat-embed-logging-juli</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat.embed</groupId>

                 <artifactId>tomcat-embed-jasper</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat</groupId>

                 <artifactId>tomcat-jasper</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat</groupId>

                 <artifactId>tomcat-jasper-el</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>

          <dependency>

                 <groupId>org.apache.tomcat</groupId>

                 <artifactId>tomcat-jsp-api</artifactId>

                 <version>${tomcat.version}</version>

          </dependency>
  • If I understood correctly your suggestion you are saying: delete all tomcat dependencies from pom.xml and add them manually. If so, how can add them manually since I am using (1) mvn clean compile, (2) mvn package and (3) java -jar \target\myExecutable.jar? As far as I know, the depencency libs are placed in myExecutable.jar/WEB-INF/libs thanks to such pom.xml lines. How would you do it manually? Would you avoid maven and follow other procedure? – Jim C Mar 09 '16 at 12:46
  • org.apache.tomcat.maven tomcat-maven-plugin 2.2 http://localhost:8080/proName/ 8080 – Akash Panday Mar 10 '16 at 04:40
  • https://apache.googlesource.com/tomcat-maven-plugin/+/tc8.x/tomcat8-maven-plugin/pom.xml – Akash Panday Mar 10 '16 at 04:41
  • I understand you give two new suggestions, I mean, you didn't showed me how you would "add the libs manually", right? Well, I know how to download them and included them in lib folders but I am really asking myself if you mean this when said "manually". In the apache.googlesource there is a much "smater" approach than mine but the maven settup dependencies are basically the same as mine. Are you suggesting me some specific point from there? For instance: managerWebAppPath or **/**IT* or java.io.tmpdir or unzip src="src/test/manager.war". Is this what you mean by "manually"? – Jim C Mar 10 '16 at 15:10
  • About tomcat-maven-plugin I have used before with no effect in my case. – Jim C Mar 10 '16 at 15:11