简体   繁体   English

如何修复maven打包的jar报告'错误:无法找到或加载主类。\\ FormulaTelemetryApp-1.0-SNAPSHOT.jar'

[英]How to fix maven-packaged jar reporting 'Error: Could not find or load main class .\FormulaTelemetryApp-1.0-SNAPSHOT.jar'

I am trying to package my JavaFX application using Maven, running it using exec:java is no problem and running maven - package yields no errors. 我正在尝试使用Maven打包我的JavaFX应用程序,使用exec运行它:java没有问题并且运行maven - package不会产生错误。 But when I want to execute the runnable jar it returns the error 'Error: Could not find or load main class .\\FormulaTelemetryApp-1.0-SNAPSHOT.jar' where the second part is the jar I want to execute. 但是当我想执行runnable jar时,它返回错误'Error:找不到或加载主类。\\ FormulaTelemetryApp-1.0-SNAPSHOT.jar',其中第二部分是我想要执行的jar。

Beneath I included my POM, I already went looking for solutions, this is where I stumbled upon the maven-jar-plugin, but including that didn't solve the problem. 在我的下面包括我的POM,我已经去寻找解决方案,这是我偶然发现maven-jar-plugin的地方,但包括那些并没有解决问题。 I also tried cleaning the project and compiling/packaging again but that didn't change anything either. 我也尝试过清理项目并再次编译/打包,但这也没有改变任何东西。

As you might notice, I am fairly new to Maven (trying to self-learn it) so there might be some strange things in the POM. 正如您可能注意到的,我对Maven相当新(尝试自学)因此POM中可能会有一些奇怪的东西。

<?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>com.formulaElectric</groupId>
    <artifactId>FormulaTelemetryApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>FormulaTelemetryApp</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.feb.telemetry.application.TelemetryApplication</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <mainClass>org.feb.telemetry.application.TelemetryApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <!-- use the following if you're not using a snapshot version. -->
        <repository>
            <id>localSnap</id>
            <name>RepositoryProxyRel</name>
            <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>
        </repository>
        <!-- use the following if you ARE using a snapshot version. -->
        <snapshotRepository>
            <id>MylocalSnap</id>
            <name>RepositoryProxySnap</name>
            <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>9.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.23</version>
        </dependency>
        <dependency>
            <groupId>com.fazecast</groupId>
            <artifactId>jSerialComm</artifactId>
            <version>1.3.11</version>
        </dependency>
        <dependency>
            <groupId>eu.hansolo</groupId>
            <artifactId>tilesfx</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0-rc1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.21.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

</project>

As stated in the comments, the problem was two-fold: on one hand a fat jar was needed, maven-shade-plugin was used for that. 正如评论中所述,问题是双重的:一方面需要一个胖罐,而maven-shade-plugin则用于此。 Besides that a launcher class (non-application extending class) was needed as main class, as is stated in the answer to this question . 除此之外,需要一个启动器类(非应用程序扩展类)作为主类,正如在这个问题的答案中所述。

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

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