【问题标题】:How to create a JAR file with all classes and byte code in intelliJ or eclipse?如何在 intelliJ 或 eclipse 中创建包含所有类和字节码的 JAR 文件?
【发布时间】:2017-07-09 03:04:56
【问题描述】:

我是 java 新手,目前正在做一个 java 项目。这是关于如何提交项目的说明。谁能告诉我如何在intelliJ或eclipse中做到这一点?

请提交 Java 存档(包含您编写的所有 Java 类的 jar 文件)。你的 jar 文件应该 包含: a) 包含所有类的源代码 b) 包含所有类的可执行文件(字节码)

【问题讨论】:

  • 你得到问题的答案了吗?如果是,请考虑接受,否则请在问题中添加更多详细信息。

标签: java eclipse intellij-idea jar bytecode


【解决方案1】:

这个问题一直是already answered heremultiple times

由于您还需要包含源代码,因此您必须更改resource patterns,以便将.java 文件也复制到编译器输出中,从而包含在.jar 文件中。

默认情况下,.java 文件被排除在复制之外,因此您需要删除排除它们的 !?*.java; 模式:

!?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj

变成

!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj

不要忘记将它改回您的实际应用程序。

如果您需要 IntelliJ IDEA 的示例项目,您可以下载它from my another answer。它显示了一个更复杂的示例,其中使用不同的方式(单个 jar 和多个 jar)将其他依赖 jar 包含到项目中。

【讨论】:

  • 为什么这会被任何人否决?我投了赞成票。
【解决方案2】:

如果您使用的是 eclipse,则可以根据您的要求采用以下方式之一。

要将您正在工作的项目导出为 jar:

1) 右键单击​​您正在工作的项目 > 从上下文菜单中选择 Export

2) 选择 Java > JAR 文件

3) 选择要导出为 JAR 的项目。输入生成的jar文件的名称,然后点击finish。

如果您使用的是 Maven,请对 pom.xml 进行以下配置

<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.xxxxx.xxxxx</mainClass>
            </manifest>
        </archive>
        <outputDirectory>C:\tmp</outputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>    
    <configuration>
        <outputDirectory>C:\tmp</outputDirectory>
    </configuration>    
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2019-01-08
    • 2010-09-13
    • 2011-01-02
    • 2012-10-14
    • 2011-06-12
    相关资源
    最近更新 更多