错误:执行jar文件时找不到或加载主类

时间:2018-07-08 14:01:11

标签: java jar executable-jar

我在maven项目中创建了两个Java文件,一个是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

也请找到我的项目结构:

enter image description here

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

要添加到@kshetline的注释中,请确保http://example.com/callback#access_token=c9186f0de67865740b9bd920a67320142434422007d16cf79031734fd450657cd4ba221106ce7232e74b7&expires_in=86400&user_id=1&email=example@mail.com 是主类的正确路径。解压缩生成的jar时,[Route("vkauth")] public class VKAuthController : Controller { [HttpGet] public string Get_VkAuth([FromQuery] string access_token, string expires_in, string user_id, string email) { } 应该位于com.es.utility.DocumentIndex文件夹中。