4

I have simple application on Spring MVC which runned on tomcat 7.0.72 in IDEA. How can I run this application on Heroku (if it's possible). I have Heroku account and I did create app on it. I use Maven for deploy and Git for push it in Heroku. Files of app will be below. So, when I run it on localhost by tomcat it works. But when I push it on heroku I get it:

Application Error:

An error occurred in the application and your page could not be served. Please try again in a few moments. If you are the application owner, check your logs for details.

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mardmitry</groupId>
    <artifactId>bestmuzon</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>BestMuzon</name>
    <!--<url>http://maven.apache.org</url>-->

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-framework-version>4.1.5.RELEASE</spring-framework-version>
        <tomcat.version>8.5.6</tomcat.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-framework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring-framework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework-version}</version>
        </dependency>

        <!--JSF (include "jsf-api" and "jsf-impl")-->
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.10</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>com.github.jsimone</groupId>
            <artifactId>webapp-runner</artifactId>
            <version>8.0.30.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>https://bestmuzonproject.herokuapp.com/</url>
                    <!--<url>http://localhost:8080/manager/text</url>-->
                    <server>TomcatServer</server>
                    <path>/mkyongWebApp</path>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals><goal>copy</goal></goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.github.jsimone</groupId>
                                    <artifactId>webapp-runner</artifactId>
                                    <version>8.0.30.2</version>
                                    <destFileName>webapp-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Structure of my app: Structure of app

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- Add Support for Spring -->
    <!-- Создает Spring Container, доступный всем сервлетам и фильтрам -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Обрабатывает все запросы. Центральное понятие-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

MainController:

package com.mardmitry.controllers;

import com.mardmitry.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView main() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userJSP", new User());
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /*как только на index.jsp подтвердится форма
    <spring:form method="post"  modelAttribute="userJSP" action="check-user">,
    то попадем вот сюда
     */
    @RequestMapping(value = "/check-user")
    public ModelAndView checkUser(@ModelAttribute("userJSP") User user) {
        ModelAndView modelAndView = new ModelAndView();   
        modelAndView.setViewName("secondPage");
        modelAndView.addObject("userJSP", user);

        return modelAndView; 
    }
}

index.jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
  <head>
    <title>Best Muzon</title>
  </head>
  <body>
  <spring:form method="post"  modelAttribute="userJSP" action="check-user">

      Name: <spring:input path="name"/> (path="" - указывает путь, используемый в modelAttribute=''. в нашем случае User.name)  <br/>
      Password: <spring:input path="password"/>   <br/>
      <spring:button>Next Page</spring:button>

  </spring:form>
  </body>
</html>

Build log in Git:

Counting objects: 359, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (282/282), done.
Writing objects: 100% (359/359), 20.70 MiB | 99.00 KiB/s, done.
Total 359 (delta 130), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Java app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Installing Maven 3.3.9... done
remote: -----> Executing: mvn -B -DskipTests clean dependency:list install
remote:        [INFO] Scanning for projects...
remote:        [INFO]
remote:        [INFO] ------------------------------------------------------------------------
remote:        [INFO] Building BestMuzon 1.0-SNAPSHOT
remote:        [INFO] ------------------------------------------------------------------------
remote:        [INFO]
remote:        [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ bestmuzon ---
remote:        [INFO] Deleting /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/target
remote:        [INFO]
remote:        [INFO] --- maven-dependency-plugin:2.3:list (default-cli) @ bestmuzon ---
remote:        [INFO]
remote:        [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bestmuzon ---
remote:        [INFO] Using 'UTF-8' encoding to copy filtered resources.
remote:        [INFO] skip non existing resourceDirectory /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/src/main/resources
remote:        [INFO]
remote:        [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ bestmuzon ---
remote:        [INFO] Changes detected - recompiling the module!
remote:        [INFO] Compiling 2 source files to /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/target/classes
remote:        [INFO]
remote:        [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ bestmuzon ---
remote:        [INFO] Using 'UTF-8' encoding to copy filtered resources.
remote:        [INFO] skip non existing resourceDirectory /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/src/test/resources
remote:        [INFO]
remote:        [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ bestmuzon ---
remote:        [INFO] No sources to compile
remote:        [INFO]
remote:        [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ bestmuzon ---
remote:        [INFO] Tests are skipped.
remote:        [INFO]
remote:        [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ bestmuzon ---
remote:        [INFO] Building jar: /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/target/bestmuzon-1.0-SNAPSHOT.jar
remote:        [INFO]
remote:        [INFO] --- maven-dependency-plugin:2.3:copy (default) @ bestmuzon ---
remote:        [INFO] Configured Artifact: com.github.jsimone:webapp-runner:8.0.30.2:jar
remote:        [INFO] Copying webapp-runner-8.0.30.2.jar to /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/target/dependency/webapp-runner.jar
remote:        [INFO]
remote:        [INFO] --- maven-install-plugin:2.4:install (default-install) @ bestmuzon ---
remote:        [INFO] Installing /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/target/bestmuzon-1.0-SNAPSHOT.jar to /app/tmp/cache/.m2/repository/com/mardmitry/bestmuzon/1.0-SNAPSHOT/bestmuzon-1.0-SNAPSHOT.jar
remote:        [INFO] Installing /tmp/build_90aea2b2f1ce2e21c7e6a87e78ae382d/pom.xml to /app/tmp/cache/.m2/repository/com/mardmitry/bestmuzon/1.0-SNAPSHOT/bestmuzon-1.0-SNAPSHOT.pom
remote:        [INFO] ------------------------------------------------------------------------
remote:        [INFO] BUILD SUCCESS
remote:        [INFO] ------------------------------------------------------------------------
remote:        [INFO] Total time: 3.629 s
remote:        [INFO] Finished at: 2016-10-20T10:34:05+00:00
remote:        [INFO] Final Memory: 22M/171M
remote:        [INFO] ------------------------------------------------------------------------
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 70M
remote: -----> Launching...
remote:        Released v23
remote:        https://bestmuzonproject.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/bestmuzonproject.git
 + 8306e15...83a9a0c master -> master (forced update)

Heroku logs:

2016-10-20T10:34:13.261601+00:00 heroku[api]: Deploy 83a9a0c by [email protected]
2016-10-20T10:34:13.261601+00:00 heroku[api]: Release v23 created by [email protected]
2016-10-20T10:34:13.409620+00:00 heroku[slug-compiler]: Slug compilation finished
2016-10-20T10:34:13.409611+00:00 heroku[slug-compiler]: Slug compilation started
2016-10-20T10:34:13.962566+00:00 heroku[web.1]: Restarting
2016-10-20T10:34:13.963376+00:00 heroku[web.1]: State changed from up to starting
2016-10-20T10:34:16.874279+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-10-20T10:34:17.556730+00:00 heroku[web.1]: Starting process with command `sh target/bin/webapp`
2016-10-20T10:34:18.183328+00:00 heroku[web.1]: Process exited with status 143
2016-10-20T10:34:19.795333+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-20T10:34:19.796435+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-20T10:34:19.682528+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2016-10-20T10:34:19.686998+00:00 app[web.1]: sh: 0: Can't open target/bin/webapp
2016-10-20T10:34:19.777080+00:00 heroku[web.1]: Process exited with status 127
2016-10-20T10:34:23.291738+00:00 heroku[web.1]: Starting process with command `sh target/bin/webapp`
2016-10-20T10:34:24.945775+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2016-10-20T10:34:24.946678+00:00 app[web.1]: sh: 0: Can't open target/bin/webapp
2016-10-20T10:34:25.024482+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-20T10:34:25.009062+00:00 heroku[web.1]: Process exited with status 127
2016-10-20T10:34:57.262474+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=bestmuzonproject.herokuapp.com request_id=1acd63ba-7e38-4279-881c-2eb1a1708a63 fwd="178.94.207.167" dyno= connect= service= status=503 bytes=
2016-10-20T10:35:38.838802+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=bestmuzonproject.herokuapp.com request_id=cafaddfe-5c81-4d00-a47d-9c1111d6053b fwd="178.94.207.167" dyno= connect= service= status=503 bytes=
7
  • Here is the guide devcenter.heroku.com/articles/… Commented Oct 20, 2016 at 10:43
  • I saw that and tried to do it. But app error. And in it guide app wrote in different maner/type. Commented Oct 20, 2016 at 10:44
  • What does it show in logs? Commented Oct 20, 2016 at 10:45
  • I added build log from Git to question. Commented Oct 20, 2016 at 10:50
  • Have you added a procfile? Commented Oct 20, 2016 at 11:18

1 Answer 1

4

There are (at least) two deployment styles for Java web applications:

The first is detailed here:

https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat

The second is detailed here:

https://devcenter.heroku.com/articles/java-webapp-runner

You seem to have mixed elements of both mechanisms in your POM: for example the webapp-runner is only required in the second style.

Remove these conflicts and check the configuration.

Sign up to request clarification or add additional context in comments.

4 Comments

So, I can't create app which can be run in IDEA by tomcat and on heroku?
I try to do it in second way.
You can deploy from IDEA : devcenter.heroku.com/articles/… OR read spring boot in action chapter 8
I want to buid app with Spring and mapping but not on servlets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.