0

I after having troubles with deploying my app on Heroku, I created the simplest application possible: it contains two files: an index.jsp and a SomeServlet.java.

I have the following project structure:

TestDeployApp
  --WEB-INF
    --classes
      --com
        --web
          --SomeServlet.class
    --web.xml

  --src
    --com
      --web
        --SomeServlet.java

  --target
    --classes
      --com
        --web
          --SomeServlet.class
    --dependency
      --webapp-runner.jar
    --maven-archiver
      --pom.properties
    --gs-serving-web-content-0.1.0.jar
    --gs-serving-web-content-0.1.0.jar.original

  --index.jsp
  --pom.xml
  --Procfile
  --TestDeployApp.war

index.jsp:

<html>
  <head></head>
  <body>it's a jsp</body>
</html>

SomeServlet.java:

package com.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


public class SomeServlet extends HttpServlet {
            public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                    PrintWriter out = response.getWriter();
            java.util.Date today = new java.util.Date();
                    out.println("<html><body><h1>" + today + "</h1></body></html");

            }
}

web.xml:

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

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>TestDeployApp</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <context-param>
            <param-name>adminPlanet</param-name>
            <param-value>Earth</param-value>
    </context-param>

    <servlet>
        <servlet-name>SomeServlet</servlet-name>
        <servlet-class>com.web.SomeServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SomeServlet</servlet-name>
        <url-pattern>/SomeServlet.do</url-pattern>
    </servlet-mapping>

</web-app>

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>org.springframework</groupId>
    <artifactId>gs-serving-web-content</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>


    </dependencies>

    <properties>
        <java.version>1.7</java.version>
        <start-class>com.web.SomeServlet</start-class>
    </properties>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </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>

        <defaultGoal>install</defaultGoal>
    </build>

</project>

Procfile:

web: java $JAVA_OPTS -cp target/classes/:target/dependency/* com.web.SomeServlet

What I'm doing to deploy it all:

git init
git add 
git commit -m "first"
git push heroku master
heroku open

After the browser window opens I see An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. error.

In logs:

2018-02-10T20:16:50.864803+00:00 app[web.1]: Error: Could not find or load main class com.web.SomeServlet
2018-02-10T20:16:56.052981+00:00 heroku[web.1]: Starting process with command `java $JAVA_OPTS -cp target/classes/:target/dependency/* com.web.SomeServlet`
2018-02-10T20:16:58.391978+00:00 heroku[web.1]: Process exited with status 1
2018-02-10T20:16:58.419611+00:00 heroku[web.1]: State changed from starting to crashed
2018-02-10T20:16:58.099331+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2018-02-10T20:16:58.103410+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -Dfile.encoding=UTF-8 
2018-02-10T20:16:58.297295+00:00 app[web.1]: Error: Could not find or load main class com.web.SomeServlet
2018-02-10T20:17:41.405484+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=testdeployapp-mine.herokuapp.com request_id=c217c572-e92a-409d-b8c8-27e57bb20013 fwd="178.217.106.173" dyno= connect= service= status=503 bytes= protocol=https
2018-02-10T20:17:42.829328+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=testdeployapp-mine.herokuapp.com request_id=21b0276c-ef8c-41b0-92c1-86c15d727aad fwd="178.217.106.173" dyno= connect= service= status=503 bytes= protocol=https
2018-02-10T20:17:58.784267+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=testdeployapp-mine.herokuapp.com request_id=a303e9da-e9d5-4d15-bd78-d3de2b3b3240 fwd="178.217.106.173" dyno= connect= service= status=503 bytes= protocol=https
2018-02-10T20:18:12.634882+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=testdeployapp-mine.herokuapp.com request_id=ce575c1b-d1ab-4f83-aaf7-a95cab4c7752 fwd="178.217.106.173" dyno= connect= service= status=503 bytes= protocol=https

I also then tried to use java -jar webapp-runner.jar application.war command, but I get an Unable to access jarfile webapp-runner.jar error in cmd.

Any suggestions are welcome. I tried all the variations of Procfile I could find, nothing seems to work.

parsecer
  • 4,758
  • 13
  • 71
  • 140

3 Answers3

3

Basically, you are mixing the war or "web application" deployment with jar or "executable" deployment. For the first one, you need to have a Web Application which can be deployed on the Servlet Container like Tomcat and the package in .war format. For this type of deployments, Heroku will require you to run your application via providing target/dependency/webapp-runner.jar to Procfile. So the idea behind it is to ask Heroku to run servlet container and deploy your .war file. So you will need to change your Procfile to

web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

But as I see you are using the Spring Boot packaging, which creates a jar packaged executable with the embedded servlet container. But you do not have any main class which will start-up spring boot application. So in your case just add a spring boot main class like:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

And then change your Procfile to web: java -jar target/your-app-name.jar

So either change the application type from Spring Boot to a usual web application and use Heroku web runner or add Spring Boot starter main class.

Babl
  • 7,446
  • 26
  • 37
0

It looks like your java classpath is not correct?

java $JAVA_OPTS -cp target/classes/:target/dependency/* com.web.SomeServlet

lawkai
  • 155
  • 1
  • 9
0

Is your port number correct while you are trying to access the app? Check your default port once. Might be localhost:8080 or other which you are not aware of.

stackFan
  • 1,528
  • 15
  • 22