java - 无法运行依赖项 main 函数

标签 java maven reflection jar dependencies

我有两个java项目,animal项目和animal-test项目。在 animal 项目中,我有一个名为 Animal 的注释界面,如下所示

Animal.java

package com.animal;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Animal{
    String name();
} 

除此之外,我还有一个接口(interface)MakeSound.java,它有一个名为ma​​kesSound()的方法。

现在我已经构建并创建了包含依赖项的 animal jar 文件,然后我在我的第二个项目 animal-test 中导入了 animal.jar>。我创建一个名为 AnimalTest.java 的类 我现在构建并创建一个 jar 文件 animal-test.jar ,当我运行 java -jar Animal-test.jar 这样的 jar 文件时,出现以下异常

no main manifest attribute, in animal-test.jar

任何人都可以帮我解决这个问题吗?

最佳答案

您还需要添加插件来在 pom.xml 中为动物测试创建 MANIFEST 文件。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.animal.AnimalFinder</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

关于java - 无法运行依赖项 main 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44550272/

相关文章:

java - 在捕获 InterruptedException 后我们真的应该中断当前线程吗?

java - 公共(public)日志记录 : Won't detect properties file

使用 JOOQ 的 java.lang.NoSuchMethodError

c# - Type.GetInterfaces() 仅用于声明的接口(interface)

java - 如何在 Android 应用程序中将 PDF 文件转换为 DOC 文件

java - 将 MS Office communicator 状态指示器添加到 JSP 中

java - hibernate-commons-annotations-4.0.1.Final.jar;无效的 LOC header (错误的签名)?

reflection - 通过反射调用数据类copy()

Java反射,改变父类(super class)中声明的字段的值

java - 搜索图标操作按钮未显示在操作栏中 - 无法访问搜索功能