Heroku:最简单的应用程序,找不到或加载主类

问题描述:

我在Heroku上部署应用程序出现问题后,创建了最简单的应用程序:它包含两个文件:index.jspSomeServlet.javaHeroku:最简单的应用程序,找不到或加载主类

我有以下项目结构

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 

我在做什么部署这一切:

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

浏览器窗口打开后,我看到An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.错误。

在日志:

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 

我也然后试图用java -jar webapp-runner.jar application.war命令,但我在cmd中得到一个Unable to access jarfile webapp-runner.jar错误。

欢迎任何建议。我尝试了所有Procfile的变体,我可以发现,似乎没有任何工作。

+1

浏览器缓存? –

+1

尝试使用隐身标签 –

+0

某些浏览器在开发人员工具处于打开状态时可以使用“禁用缓存”选项 –

基本上,您正在混合war或“web应用程序”部署与jar或“可执行”部署。对于第一个,您需要有一个Web应用程序,它可以像Tomcat一样部署在Servlet容器上,也可以以.war格式包装。对于这种部署,Heroku将要求您通过提供target/dependency/webapp-runner.jarProcfile来运行您的应用程序。因此,它背后的想法是要求Heroku运行servlet容器并部署您的文件.war。 所以,你需要将Procfile改变

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

但是当我看到你使用的是Spring引导包装,它创建与嵌入式servlet容器中jar打包可执行文件。但是你没有任何启动spring引导应用程序的主要类。所以你的情况只是增加一个弹簧启动主类,如:

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

,然后改变你的Procfile到 web: java -jar target/your-app-name.jar

所以要么改变从春天启动的应用程序类型,通常Web应用程序和使用Heroku的网页亚军或者添加Spring Boot starter主类。

它看起来像你的java类路径不正确?

的Java $ JAVA_OPTS -cp目标/班/ :目标/依赖/ * com.web.SomeServlet

是您的端口号正确,当您试图访问该应用程序?检查您的默认端口一次。可能是localhost:8080或其他你不知道的。