简体   繁体   English

在 Heroku 上部署 Java Telegram bot:找不到或加载主 class

[英]Deploy Java Telegram bot on Heroku: Could not find or load main class

I'm writing a telegram bot by this lib: https://github.com/rubenlagus/TelegramBots .我正在用这个库写一个电报机器人: https://github.com/rubenlagus/TelegramBots

The bot can run successfully on my local machine but cannot run on Heroku.该机器人可以在我的本地机器上成功运行,但无法在 Heroku 上运行。 The Error Message is "Counld not find or load main class."错误消息是“无法找到或加载主 class”。

I have read a lot of similar questions but still cannot solve my problem because other questions have some differences from this.我已经阅读了很多类似的问题,但仍然无法解决我的问题,因为其他问题与此有所不同。

Here are some logs when the error happened on Heroku:以下是 Heroku 发生错误时的一些日志:

2021-05-12T11:51:15.644410+00:00 heroku[worker.1]: Starting process with command `sh target/bin/my_bot_test`
2021-05-12T11:51:17.945513+00:00 heroku[worker.1]: State changed from starting to up
2021-05-12T11:51:19.427171+00:00 app[worker.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2021-05-12T11:51:19.616191+00:00 app[worker.1]: Error: Could not find or load main class Main
2021-05-12T11:51:19.668857+00:00 heroku[worker.1]: Process exited with status 1
2021-05-12T11:51:19.756822+00:00 heroku[worker.1]: State changed from up to crashed

Here is the content of pom.xml :这是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.example</groupId>
    <artifactId>my_bot_test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>5.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots-abilities</artifactId>
            <version>5.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.30</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.1.1</version>
                <configuration>
                    <assembleDirectory>target</assembleDirectory>
                    <programs>
                        <program>
                            <mainClass>Main</mainClass>
                            <name>my_bot_test</name>
                        </program>
                    </programs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Here are some cmds are used for deploy:以下是一些用于部署的 cmd:

mvn clean install
git add .
git commit -m "test"
heroku login
git remote add heroku [gitUrl]
git push heroku master
heroku ps:scale worker=1

I referred to this page when studying: https://javarush.ru/groups/posts/504-sozdanie-telegram-bota-na-java-ot-idei-do-deploja我在学习时参考了这个页面: https://javarush.ru/groups/posts/504-sozdanie-telegram-bota-na-java-ot-idei-do-deploja

The Procfile I wrote this:我写的Procfile:

worker: sh target/bin/my_bot_test

I learned that the content of Procfile means excute command target/bin/my_bot_test in the root directory of project.我了解到Procfile的内容是指在项目的根目录中执行命令target/bin/my_bot_test So I try to excute it on my machine and it runs successfully.所以我尝试在我的机器上执行它并成功运行。

I don't how the error happened and how to solve the problem... Please help me.我不知道错误是如何发生的以及如何解决问题...请帮助我。 Thanks in advance.提前致谢。

The problem was solved.问题解决了。

The cause of this is Heroku will compile the source code on server not run compiled program directly.造成这种情况的原因是 Heroku 将在服务器上编译源代码而不是直接运行已编译的程序。 So I should push THE SOURCE CODE rather than the complied program to the git of Heroku.所以我应该将源代码而不是编译好的程序推送到 Heroku 的 git 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM