【问题标题】:Heroku fails to find class in jar fileHeroku 无法在 jar 文件中找到类
【发布时间】:2016-10-13 11:14:19
【问题描述】:

我正在向 Heroku 部署一个 jar 文件,但 Heroku 经常抱怨找不到类文件(在依赖项 jar 中)。我已经在我的 jar 中包含了这个类,它在本地运行时可以完美运行,但是当我在 Heroku 上运行它时,它会吐出错误:

2016-06-13T06:04:30.743345+00:00 app[web.1]: Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
2016-06-13T06:04:30.743349+00:00 app[web.1]:    at java.lang.Class.getDeclaredMethods0(Native Method)
2016-06-13T06:04:30.744920+00:00 app[web.1]:    at java.lang.Class.getMethod0(Class.java:3018)
2016-06-13T06:04:30.744922+00:00 app[web.1]:    at java.lang.Class.getMethod(Class.java:1784)
2016-06-13T06:04:30.744923+00:00 app[web.1]:    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
2016-06-13T06:04:30.744926+00:00 app[web.1]:    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
2016-06-13T06:04:30.744927+00:00 app[web.1]:    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
2016-06-13T06:04:30.745502+00:00 app[web.1]:    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
2016-06-13T06:04:30.745510+00:00 app[web.1]:    ... 7 more
2016-06-13T06:04:31.375413+00:00 heroku[web.1]: Process exited with status 143
2016-06-13T06:04:31.774220+00:00 heroku[web.1]: Process exited with status 1
2016-06-13T06:04:31.789553+00:00 heroku[web.1]: State changed from starting to crashed

我已经在我的 jar 文件中包含了 Maven 依赖项以及实际的 jar。 Heroku 是否需要在部署之前将依赖项放在其他地方?任何帮助将非常感激。谢谢。

编辑:有谁知道 Heroku 在哪里检查类文件?

【问题讨论】:

  • 你创建了 fat-jar 吗? [包含所有依赖项的单个 jar]
  • 如何在本地运行应用程序?您如何组装应用程序?
  • 也请告诉我们您的Procfile

标签: java maven heroku


【解决方案1】:

如果它是 springboot 应用程序,请确保您的 pom 标签如下所示:

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

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

【讨论】:

  • “看起来像这样”听起来很模糊,能否请您说的更具体些?
  • 这里的目标“重新打包”创建一个可执行的 jar 文件,所以如果我没记错,在 heroku 上运行它不会崩溃这是我在解决这个问题之前添加的额外内容:&lt;execution&gt; &lt;goals&gt; &lt;goal&gt;repackage&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; 同时检查你的 Procfile 是否上传项目后,您在 heroku 上运行的命令如下所示:web: java -Dserver.port=$PORT -jar target/&lt;jar_name&gt;.jar
猜你喜欢
  • 1970-01-01
  • 2011-08-26
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 2017-02-07
  • 2012-10-13
  • 2013-07-25
相关资源
最近更新 更多