java 创建没有主类的 Jar 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30174865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:35:44  来源:igfitidea点击:

Create Jar Library Without a Main Class

javaintellij-ideajarmain

提问by Thibstars

Today I have just created a Java Library. I created it using a Main class, since IntelliJ IDEA 14 asked me to add one. However I want it to be a normal library, without any Main classes. Is it possible to create a jar file from such a project without having a single class with the main method? If so, how do you create such a jar.

今天我刚刚创建了一个 Java 库。我使用 Main 类创建了它,因为 IntelliJ IDEA 14 要求我添加一个。但是我希望它是一个普通的库,没有任何 Main 类。是否可以从这样的项目创建一个 jar 文件,而没有一个带有 main 方法的类?如果是这样,您如何创建这样的罐子。

It just seems a bit silly to have a main method if you never use it.

如果你从不使用它,那么拥有一个 main 方法似乎有点愚蠢。

采纳答案by dey

You can do it in few ways, for example from command line, from IDE, maven or other build tool, I describe 2 ways:

您可以通过几种方式来完成,例如从命令行、IDE、maven 或其他构建工具,我描述了两种方式:

Command line:

命令行:

You can create jar file from command line (without IDE), Here is reference: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

您可以从命令行(不带 IDE)创建 jar 文件,参考:https: //docs.oracle.com/javase/tutorial/deployment/jar/build.html

jar cf jar-file input-file(s)

where jar-fileis .jar file name you want and input-file(s)are files you want to put inside your library (can be a wildcard, e.g.: *.class)

这里jar-file是你想和.jar文件名input-file(s)是文件,你想要把你的库中(可使用通配符,如:*.class

Intellij Idea:

智能理念:

Create Artifact like in this article, but without specifying Main class http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/

像本文一样创建 Artifact,但不指定 Main 类http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/

Then click Build > Build artifact > Build.

然后单击Build > Build artifact > Build

This works even if there is no Main class.

即使没有 Main 类,这也有效。

回答by Darrell Teague

Use a build tool like Maven (no IDE dependencies but can be called from IDE for convenience) with the shade plugin to create an 'uber' JAR (that includes all needed dependencies into one final JAR for the project)...

使用像 Maven 这样的构建工具(没有 IDE 依赖项,但可以从 IDE 调用以方便起见)和 shade 插件来创建一个“超级”JAR(将所有需要的依赖项包含到项目的一个最终 JAR 中)...

"pom.xml"

“pom.xml”

...

...

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
       <!-- Run shade goal on package phase -->
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Documentation to Shade plugin:

Shade插件的文档:

https://maven.apache.org/plugins/maven-shade-plugin/

https://maven.apache.org/plugins/maven-shade-plugin/