【问题标题】:Error: Could not find or load main class while executing jar file错误:执行 jar 文件时无法找到或加载主类
【发布时间】:2018-12-16 09:09:11
【问题描述】:

我创建了两个 java 文件,我的 maven 项目一个是 POJO 类,另一个是 java 主类文件。我想将我的项目作为一个可执行的 jar 文件,我想使用 java -jar 命令在外部运行。

请找到我的 pom.xml 文件

<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>ElasticSearchUtility</groupId>
    <artifactId>ElasticSearchUtility</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>rest</artifactId>
            <version>5.1.2</version>
        </dependency>    
    </dependencies>

    <build>    
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- Make this jar executable -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/log4j.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.es.utility.DocumentIndex</mainClass>
                            <classpathPrefix>dependency-jars/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- Copy project dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- exclude junit, we need runtime dependency only -->
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

在使用java -jar 命令执行我的 jar 文件时。我得到了错误

Error: Could not find or load main class com.es.utility.DocumentIndex

还请找到我的项目结构:

【问题讨论】:

  • 您是否检查过您的.jar 文件以查看您的主类是否在.jar 文件中以及正确的路径中?如果您以前没有这样做过,可以将.jar 更改为.zip 以帮助检查内容。
  • @kshetline - 提取 .jar 文件后,我只有 META-INF 文件夹
  • 我不知道你使用的这个特定的构建工具是如何工作的,但你可能错过了&lt;archive&gt; 部分中将所有类文件放入存档的步骤。
  • 你的项目的文件夹结构是什么?
  • 离题评论:删除maven-eclipse-pluigin。它不再被维护,并且不能很好地与现代版本的 Eclipse 和 Maven 配合使用。见Apache Maven Eclipse Plugin (RETIRED)

标签: java jar executable-jar


【解决方案1】:

要添加到@kshetline 的评论,请确保com.es.utility.DocumentIndex 是您的主类的正确路径。解压生成的jar包时,DocumentIndex.class应该在com/es/utility文件夹中。

【讨论】:

  • 提取 .jar 文件后,我只有 META-INF 文件夹
  • 听起来你的 Maven 构建是 1) 没有构建你的代码,2) 没有将构建的工件放在打包步骤所期望的位置,或者 3) 没有调用打包步骤。
【解决方案2】:

如果 .jar 中没有类,可能是因为 maven 找不到它们。 Maven 中 java 类的默认路径是 src/main/java,尝试将它们移到那里并再次运行 Maven。

【讨论】:

  • 它解决了我的问题,但现在我遇到了不同的问题Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/Ela sticsearchException 我已经为此创建了一个单独的问题。 stackoverflow.com/questions/51240395/…
猜你喜欢
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2015-07-27
  • 2014-09-04
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
相关资源
最近更新 更多