Heroku上的应用程序错误无法找到或加载主类

时间:2016-11-02 17:18:00

标签: java spring heroku

我在Spring MVC和Tomcat上做了简单的应用程序。它还与DB连接。我的应用程序代码在底部。当我在PC上运行它时它工作得很完美但是当我在heroku上部署它时我得到了一个

  

应用程序错误:

     

应用程序中发生错误,您的页面无法执行   提供服务。请稍后重试。如果你是申请人   所有者,请查看日志以获取详细信息。

我推送时有错误,下面是我的heroku日志:

2016-11-02T18:12:07.193103+00:00 heroku[web.1]: Starting process with command `java -cp target/classes/:target/dependency/* com.shop.cond.MainController`
2016-11-02T18:12:09.518875+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2016-11-02T18:12:09.522830+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx350m -Xss512k -Dfile.encoding=UTF-8
2016-11-02T18:12:09.720990+00:00 app[web.1]: Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/ui/Model
2016-11-02T18:12:09.715926+00:00 app[web.1]: Error: A JNI error has occurred, please check your installation and try again
2016-11-02T18:12:09.721171+00:00 app[web.1]:    at java.lang.Class.getDeclaredMethods0(Native Method)
2016-11-02T18:12:09.721268+00:00 app[web.1]:    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
2016-11-02T18:12:09.721330+00:00 app[web.1]:    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
2016-11-02T18:12:09.721391+00:00 app[web.1]:    at java.lang.Class.getMethod0(Class.java:3018)
2016-11-02T18:12:09.721513+00:00 app[web.1]:    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
2016-11-02T18:12:09.721451+00:00 app[web.1]:    at java.lang.Class.getMethod(Class.java:1784)
2016-11-02T18:12:09.721715+00:00 app[web.1]: Caused by: java.lang.ClassNotFoundException: org.springframework.ui.Model
2016-11-02T18:12:09.721576+00:00 app[web.1]:    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
2016-11-02T18:12:09.721778+00:00 app[web.1]:    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
2016-11-02T18:12:09.721838+00:00 app[web.1]:    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
2016-11-02T18:12:09.721902+00:00 app[web.1]:    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
2016-11-02T18:12:09.721988+00:00 app[web.1]:    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
2016-11-02T18:12:09.722057+00:00 app[web.1]:    ... 7 more
2016-11-02T18:12:09.843097+00:00 heroku[web.1]: State changed from starting to crashed
2016-11-02T18:12:09.847741+00:00 heroku[web.1]: Process exited with status 1
2016-11-02T18:12:11.191694+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=bestmuzonproject.herokuapp.com request_id=0c01c995-e9b1-4c86-84bb-9b4abbdcabd4 fwd="178.94.207.167" dyno= connect= service= status=503 bytes=
2016-11-02T18:12:11.429322+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=bestmuzonproject.herokuapp.com request_id=cf7c5536-8106-46e5-9e4c-db48d17242cf fwd="178.94.207.167" dyno= connect= service= status=503 bytes=

它可以是什么?为什么我会收到这个错误?

我的代码。主控制器:

package com.shop.cond;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.shop.cond.model.Product;
import com.shop.cond.service.ProductService;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/")
public class MainController {

    private ProductService productService;

    @Autowired(required=true)
    @Qualifier(value="productService")
    public void setPersonService(ProductService ps){
        this.productService = ps;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        return "index";
    }

    @RequestMapping(value =  {"/home"}, method = RequestMethod.GET)
    public String homePage() {
        System.out.println("home");
        return "forward:/";
    }

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public String aboutPage(Model model) {
        System.out.println("about");
        return "forward:/";
    }

    @ResponseBody
    @RequestMapping(value = "/products/listProducts", method = RequestMethod.GET)
    public List<Product> getProducts() {
        List<Product> products = this.productService.listProducts();
        return products;
    }

    @RequestMapping(value =  {"/products"}, method = RequestMethod.GET)
    public String productsPage() {
        System.out.println("products");
        return "forward:/";
    }

    @RequestMapping(value =  {"/contacts"}, method = RequestMethod.GET)
    public String contactsPage() {
        System.out.println("contacts");
        return "forward:/";
    }
}

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.shop.cond</groupId>
    <artifactId>OnlineShop</artifactId>
    <name>OnlineShop</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>4.3.5.Final</hibernate.version>
    </properties>
    <dependencies>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <!-- Spring -->

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

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>


        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--JSON Response-->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.7.1</version>
        </dependency>

        <!--PostgreeSQL-->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>


            <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>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>






            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>OnlineShop</server>
                    <path>/</path>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <assembleDirectory>target</assembleDirectory>
                    <programs>
                        <program>
                            <mainClass>com.shop.cond.MainController</mainClass>
                            <name>webapp</name>
                        </program>
                    </programs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>
</project>

的servlet上下文:

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.shop.cond" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources
        in the /WEB-INF/views directory -->
    <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!--<beans:property name="url" value="jdbc:postgresql://ec2-54-235-177-62.compute-1.amazonaws.com:5432/d7pgi7nq0ue78j" />-->
        <!--<beans:property name="username" value="htcxkqyeyymcxz" />-->
        <!--<beans:property name="password" value="rs7Z8b4ULNa5zol8fSzJd_BNNR" />&ndash;&gt;
-->
        <beans:property name="url" value="jdbc:mysql://localhost:3306/shopDB" />
        <beans:property name="username" value="arxangel192" />
        <beans:property name="password" value="qwe321zxc321" />
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.shop.cond.model.Product</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="productDAO" class="com.shop.cond.dao.ProductDAOImpl">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="productService" class="com.shop.cond.service.ProductServiceImpl">
        <beans:property name="productDAO" ref="productDAO"/>
    </beans:bean>
    <context:component-scan base-package="com.shop.cond" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>


</beans:beans>

</beans:beans>

Procfile:

web: sh target/bin/webapp
web: java -cp target/classes/:target/dependency/* com.shop.cond.MainController

目录结构: STRUCTURE

1 个答案:

答案 0 :(得分:0)

首先,web:中有两个Procfile条目。如果您需要,请仅使用java -cp

主要错误是java.lang.NoClassDefFoundError: org/springframework/ui/Model,这通常意味着您在类路径中缺少JAR文件。

您是否已将所有依赖项复制到target/dependency

通常,您可以在pom.xml中使用以下内容:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals><goal>copy-dependencies</goal></goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果您有该配置,请检查您的Spring依赖项是否未设置为provided