0

I'm using IntelliJ Idea (14.1.2) to develop a simple java command-line application.

My application is composed of two modules:

  • A main module that just contains the "main" class
  • A library module that contains the actual code that "does things" (this module also includes and use a maven library)

Everything works when debugging from within IntelliJ, but now I want to build a JAR file in order to use the tool outside of the IDE. So, as suggested by the IntelliJ documentation, I used the "Artifacts" menu to setup a JAR artifact to be generated at build time.

The JAR seems to build correctly, but it doesn't. The problem is this: the code from my library module is NOT included in the jar, only the code from the main module is! In fact, strangest of all, the code from the maven library which my library module includes is included in the jar, but not the code of my module library itself! This obviously means that the final jar throws a ClassNotFound exception as soon as the main program tries to use a class from the library module.

I don't know what I am doing wrong, the dependencies are all set, in the Artifact creation menu I told it to include the dependencies in the jar...

EDIT: Here's the script created by IntelliJ, all seems in order:

<component name="ArtifactManager">
      <artifact type="jar" build-on-make="true" name="delta.desktoptools.manifestgenerator:jar">
        <output-path>$PROJECT_DIR$/out/artifacts</output-path>
        <root id="archive" name="delta.desktoptools.manifestgenerator.jar">
          <element id="module-output" name="delta.desktoptools.manifestgenerator" />
          <element id="module-output" name="delta.desktoptools.lib" /> <!-- This doesn't get included :( -->
          <element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/github/javaparser/javaparser-core/2.0.0/javaparser-core-2.0.0.jar" path-in-jar="/" />
        </root>
      </artifact>
    </component>

EDIT: A screen of my artifact configuration, for better clarity. As you can see the compilation output of my library module is included in the artifact, but is absent from the final JAR. enter image description here

Master_T
  • 7,232
  • 11
  • 72
  • 144

2 Answers2

0

are you using intellij to make the Jar or relying on the maven task?

If you are relying on the pom then you need to add the build with dependencies stuff as detailed here How can I create an executable JAR with dependencies using Maven?

Community
  • 1
  • 1
Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
  • I'm using IntelliJ itself. I never used maven to be honest, is it required to do this properly? Is the Artifacts functionality included in IntelliJ buggy/incomplete? All the documentation I've read seems to suggest that IntelliJ _should_ include the needed dependencies... but it doesn't :( – Master_T Apr 27 '15 at 10:10
  • I think IntelliJ will add only the dependencies included in the Project, and if it doesn't you will have them available in the "Available Elements" panel, so all you need to do is drag it to the left so it's included in the artifact (in this case the JAR file) – Rafael R. S. Robles Apr 27 '15 at 20:04
  • I did... I added a screenshot and the script generated by IntelliJ... it looks correct to me :/ – Master_T Apr 28 '15 at 07:39
0

Ok, I solved it.

I was using JDK 1.7.x, IntelliJ Artifacts creator must have some incompatibility/bug with it (weird since I'm pretty sure the Artifacts generator pre-dates JDK 1.7 by quite a bit, but whatever).

Switching to JDK 1.8.x solved the issue (either that or it triggered a "deep cleanup" of the project or something to that effect which effectively fixed the problem)

EDIT: After further testing, turns out JDK version has nothing to do with it, I was just testing various things and I thought the solution was that, but it wasn't. Apparently, the problem was that IntelliJ didn't "like" the name of my library O.o I renamed it from delta.desktoptools.lib to delta.desktoptools.library and now it gets included. Weird, but at least it works. Would be interesting to know why. Maybe packages ending in .lib trigger some kind of name convention/reservation rule? I'm not familiar enough with IntelliJ to know for sure...

Master_T
  • 7,232
  • 11
  • 72
  • 144
  • @kuporific: You have to wait 24hrs to accept your own answer... PS: I see you're quite active on questions about IntelliJ, any clues why this happens? Even if I solved it I don't know why exactly :D – Master_T Apr 28 '15 at 14:09