How to build jars from IntelliJ properly?

前端 未结 20 2496
盖世英雄少女心
盖世英雄少女心 2020-11-22 01:14

I have a project that contains a single module, and some dependencies. I\'d like to create a jar, in a separate directory, that contains the compiled module. In addition, I\

相关标签:
20条回答
  • 2020-11-22 01:55

    Ant and Maven are widely used. I prefer Ant, I feel it's more lightweight and you the developer are more in control. Some would suggest that's its downside :-)

    0 讨论(0)
  • 2020-11-22 01:58

    Here is the official answer of IntelliJ IDEA 2018.3 Help. I tried and It worked.

    To build a JAR file from a module;

    1. On the main menu, choose Build | Build Artifact.

    2. From the drop-down list, select the desired artifact of the type JAR. The list shows all the artifacts configured for the current project. To have all the configured artifacts built, choose the Build all artifacts option.

    0 讨论(0)
  • 2020-11-22 01:59

    I recently had this problem and think these steps are easy to follow if any prior solution or link is missing detail.

    How to create a .jar using IntelliJ IDEA 14.1.5:

    1. File > Save All.
    2. Run driver or class with main method.
    3. File > Project Structure.
    4. Select Tab "Artifacts".
    5. Click green plus button near top of window.
    6. Select JAR from Add drop down menu. Select "From modules with dependencies"
    7. Select main class.
    8. The radio button should be selecting "extract to the target JAR." Press OK.
    9. Check the box "Build on make"
    10. Press apply and OK.
    11. From the main menu, select the build dropdown.
    12. Select the option build artifacts.
    0 讨论(0)
  • 2020-11-22 01:59

    Here are 2 examples with maven project, step by step:

    Method 1: Build jar with maven and pom.xml

    1. File, new, project, maven
    2. create a package , create a java program inside that package
    3. at pom.xml, add maven-jar-plugin.

          <plugin>
              <!-- Build an executable JAR -->
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.1.0</version>
              <configuration>
                  <archive>
                      <manifest>
                          <addClasspath>true</addClasspath>
                          <classpathPrefix>lib/</classpathPrefix>
                          <mainClass>somePackage.sample</mainClass>
                      </manifest>
                  </archive>
              </configuration>
          </plugin>
      

    screen capture: 4. open maven project box by click on the search icon and type maven,

    1. click on clean, then click on install

    2. your jar file will show up inside the target folder

    3. test your jar using java -jar

    Method 2: Build jar with maven without pom.xml change

    1. File, new, project, maven
    2. create a package , create a java program inside that package (same as above)
    3. File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies

    Very important!

    The MANIFEST.MF needs to be in your resources folder and the Main.Class needs to refer to {package}-{class-name-that-contains-main-class}

    1. then click build on menu , build artifacts, build

    1. find the jar in the out folder

    1. run it :)

    0 讨论(0)
  • 2020-11-22 01:59

    As the people above says, but I have to note one point. You have to check the checkbox:

    Include in project build

    0 讨论(0)
  • 2020-11-22 01:59

    In case you are reading this answer because you are facing "resource file not found" error, try this:

    1. File -> Project Structure -> Artiface -> New, type is Other.
    2. Give it a nice name, and in the Output Layout, press Create Archive to create a new jar, and again, give it a nice name ;)
    3. Click on the jar you just added, add your compiled module output in the jar.
    4. Click on the jar again, in the lower pane, select your project path and add Manifest File then set correct Main Class and Class Path.
    5. Click Add Library Files, and select libraries you need (you can use Ctrl+A to select all).
    6. Build -> Build Artifact -> Clean & Build and see if the error is gone.
    0 讨论(0)
提交回复
热议问题