Java Maven没有在我的jar中使用我的类路径
我正在尝试使用Maven和命令行在Java中设置一个hello world示例,除了最后的.jar文件看起来是错误的之外,其他一切都很好地工作 我的项目具有以下结构: ./pom.xml ./src/main/java/my/project/path/example/example.class 我的pom.xml文件如下所示:Java Maven没有在我的jar中使用我的类路径,java,maven,Java,Maven,我正在尝试使用Maven和命令行在Java中设置一个hello world示例,除了最后的.jar文件看起来是错误的之外,其他一切都很好地工作 我的项目具有以下结构: ./pom.xml ./src/main/java/my/project/path/example/example.class 我的pom.xml文件如下所示: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o
<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>my.project.path</groupId>
<artifactId>example</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>jar</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>my.project.path.example.Example</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.0.0
my.project.path
例子
0.1
罐子
org.apache.maven.plugins
maven编译器插件
3.1
1.6
1.6
org.apache.maven.plugins
maven源插件
2.2.1
罐子
罐子
org.codehaus.mojo
execmaven插件
1.2.1
my.project.path.example.example
我的最后一个.jar文件的结构如下:
<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>my.project.path</groupId>
<artifactId>example</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>jar</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>my.project.path.example.Example</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
/例如:class
/META-INF/
我希望它是:
./my/project/path/example/example.class
/META-INF/
Java7和ApacheMaven3.0.4
我遗漏了什么吗?如果您有清单问题(查找主类),我个人会使用maven编译器插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<archive>
<manifest>
<mainClass>br.usp.lta.Example</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
org.apache.maven.plugins
maven编译器插件
2.3.2
1.7
1.7
br.usp.lta.示例
此外,确保顶部的结构与以下内容匹配:
<groupId>my.usp</groupId>
<artifactId>lta</artifactId>
<packaging>jar</packaging>
my.usp
lta
罐子
然后您的主类将位于
src/main/java/my/usp/lta/Example.class
中。使用您的pom,这对我来说很好。如果您有依赖项,您可以使用maven依赖项插件
来配置您的主jar以在类路径中引用。这是在主jar的清单文件中配置的。
maven jar插件
有助于创建可执行jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.MyMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
org.apache.maven.plugins
maven依赖插件
复制依赖项
准备包装
复制依赖项
${project.build.directory}/lib
假的
假的
真的
org.apache.maven.plugins
maven jar插件
真的
解放党/
com.example.MyMain
在包名中使用Java保留字:“class”。那不会编译。
在pom文件中,您需要添加maven jar插件。除了编译器插件,这就是你所需要的。例如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>my.path.example.Example</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
org.apache.maven.plugins
maven jar插件
2.4
my.path.example.example
我忘记了文件顶部的“package”声明。根据Java语言规范:
7.4.2。未命名的包
没有包声明的编译单元是
未命名的包
Java SE平台提供的未命名包主要用于
在开发小型或临时应用程序或
刚刚开始发展
未命名的包不能有子包,因为
包声明始终包含对命名顶级的引用
包裹
因此,添加以下行解决了问题:
package my.project.path.example;
这不是我想要的。我更新了我的问题,让它更清楚。谢谢您的实际类路径是什么,以及
src/main/java/
文件夹的内容是什么?哦,等一下,我看到了您的问题。这只是一个示例:它不是真实的。我将在问题中对其进行更改。它进行编译,只是无法使用正确的类路径创建.jar文件。