Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Eclipse ClassNotFoundException如何使jar可执行_Eclipse_Maven - Fatal编程技术网

Eclipse ClassNotFoundException如何使jar可执行

Eclipse ClassNotFoundException如何使jar可执行,eclipse,maven,Eclipse,Maven,我是Maven的新手,我有以下问题。我在另一个项目中添加了一个项目a作为依赖项 这就是我在pom中添加的内容 </build> <dependencies> <dependency> <groupId>the id</groupId> <artifactId>artifact id</artifactId> <version> the version&l

我是Maven的新手,我有以下问题。我在另一个项目中添加了一个项目a作为依赖项

这就是我在pom中添加的内容

</build>


  <dependencies>
  <dependency>
      <groupId>the id</groupId>
      <artifactId>artifact id</artifactId>
      <version> the version</version>
      <scope>compile</scope>
    </dependency>
那么我错过了什么?
任何建议都会很有帮助

Eclipse会在后台自动编译。结果是,依赖项的jar文件可能在Eclipse中可用,但在本地maven存储库中不可用。首先尝试在本地依赖项上的终端中运行“maven clean install”(这将把它添加到本地maven存储库中),然后使用另一个“maven clean install”构建下游项目


这将在maven项目根目录下的“target”目录中创建一个jar文件。

当您希望以以下方式运行应用程序时:

java -jar yourapp.jar
你必须注意两件事:

  • 确保在MANIFEST.MF中列出了您的主类,我相信您已经这样做了

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>your.package.YourMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    
    
    org.apache.maven.plugins
    maven jar插件
    2.4
    你的.package.YourMainClass
    
  • 确保在运行应用程序时正确构建了类路径。有两种方法可以做到这一点,但最简单的方法是使用shade插件。它将解压所有依赖项并将它们放在一个jar中

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    
    
    org.apache.maven.plugins
    maven阴影插件
    2.3
    包裹
    阴凉处
    

  • 如果您需要不使用shade插件的其他解决方案,请告诉我。

    这是本地依赖还是远程依赖?谢谢您的回答!我已经做到了,我只是再做一次,但我仍然有同样的错误
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>