1

I've been working on a small project and I'd like to have the .jar in the root of the repo (here's my repo so you can understand what I mean). Currently, I can only get the .jar to run when it is located in the out/artifacts/ folder (which is not shown on the GitHub repo I linked because there aren't currently any contents there). When I place it there, the necessary surrounding folders aren't there unless I move them there and I don't really want to duplicate the files within my repo. I can't figure out why this is happening. It works fine in Intellij when I run it through my main method (located in src/Main.java). I'd also like to not have to worry about my .jar and my .java files not referencing the same files with the same paths, so having the .jar in the root is pretty important to me.

Here's how I'm building my .jar.

Here are the remaining dependencies listed for the .jar to be built.

If anyone could explain to me what I'm doing wrong in terms of dependencies and getting my .jar to work, that would be great.

  • 1
    Jars don't contain jars. You need to use maven to create a "fat jar". [For example](https://stackoverflow.com/a/61390701/2970947). – Elliott Frisch Jul 28 '20 at 00:32
  • I created this as a standard Java project. Would that be an issue for using Maven as a fat jar? Or is that limited to maven projects? – Sixstringcal Jul 28 '20 at 20:43

1 Answers1

0

JAR artifact configuration is incorrect. You are packing other dependency jars directly into the main jar which is not supported by Java. Other jars either need to be unpacked (extracted) inside the main jar or linked to it via Manifest.MF file. It appears that your manifest specifies the correct classpath relative to the location of the main jar file, so it works when you run it in that directory as its able to load the linked jars from the disk. When you move the main jar to some other directory, but not move the linked dependencies with it, it will not run.

See this answer for the explanation how it works and for the sample project that you can experiment with.

See also these answers to be aware of the potential issues:

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • Seems to have helped. Thanks for helping me understand how this works much better. I've never tried running anything outside my IDE using dependencies before. The errors I get now are different, as the .jar gets further into the program, but still has errors that running the code from Main in Intellij does not give. (Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/batik/dom/util/DocumentFactory at MainMenu.liveUpdate(MainMenu.java:113)). This exception seems to be referring to the PNGTranscoder, which the appropriate dependency is extracted inside the .jar. – Sixstringcal Jul 28 '20 at 20:39
  • Most likely some of the dependencies are not inside the final jar. You may consider using Gradle or Maven to build the jar using fatjar plug-in. – CrazyCoder Jul 28 '20 at 20:44