S
S
stanmb2020-10-19 11:26:32
Java
stanmb, 2020-10-19 11:26:32

Why is the .jar not being run from the console?

When I try to run the Main class java Mainor run a .jar file , I get an errorjava -jar Bot-1.jar

Error: Unable to initialize main class Main
Caused by: java.lang.NoClassDefFoundError: org/telegram/telegrambots/meta/exceptions/TelegramApiRequestException

If you run the project in Intellij idea, then everything is ok.

pom file looks like this:
<?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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <groupId>TestBot</groupId>
    <artifactId>Bot</artifactId>
    <version>1.0</version>
    <dependencies>


    <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambots</artifactId>
        <version>4.9.1</version>
    </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.16</version>
        </dependency>

    </dependencies>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>

</project>

Please help solve the problem!
Do I understand correctly that the error occurs due to the fact that I do not have the .jar telegrambots.meta archive locally? Why is it not downloaded automatically when installing mvn install, because the dependency is registered in pom?
Where do I need to put the libraries so that the compiled .jar will run not only locally but also when deployed, for example, to herku?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-10-19
@stanmb

Through Idea, this all works because when it starts, it carefully writes the path to the necessary libraries in the classpath. All these libraries are obviously downloaded automatically and are present in the local cache (folder ~/.m2).
You need to either build "fat jar" aka jar-with-dependencies,
or carry all the necessary libs with you and add them to the classpath at startup.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question