0

I have written a javafx app and build an jar file through the "build artifacts" function of Intellij. Now if I run the jar file in the output folder like this:

java -jar IdeaProjects/Transclip/out/artifacts/Transclip/Transclip.jar

everything works perfectly, but if I copy it somewhere else:

cp  IdeaProjects/Transclip/out/artifacts/Transclip/Transclip.jar  .IdeaProjects/Transclip/out/artifacts/Transclip/Transclip.jar -> ./Transclip.jar
java -jar Transclip.jar

Then I get a whole lot of errors:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class sample.Main
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:884)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.sun.javafx.application.LauncherImpl$7.run(LauncherImpl.java:791)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/collections4/BidiMap
    at sample.Main.<init>(Main.java:48)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.BidiMap
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 12 more
Exception running application sample.Main

Any idea what is going on here?

Here is the content of MANIFEST.MF:

Manifest-Version: 1.0
Main-Class: sample.Main

Here is the file structure:

.
├── Transclip.iml
├── libs
│   ├── commons-collections4-4.0.jar
│   └── gson-2.2.4.jar
├── out
│   ├── artifacts
│   │   ├── Transclip
│   │   │   ├── Transclip.html
│   │   │   ├── Transclip.jar
│   │   │   ├── Transclip.jnlp
│   │   │   ├── commons-collections4-4.0.jar
│   │   │   └── gson-2.2.4.jar
│   │   └── Transclip_jar
│   │       └── Transclip.jar
│   └── production
│       └── Transclip
│           ├── META-INF
│           │   └── MANIFEST.MF
│           ├── com
│           │   ├── detectlanguage
│           │   │   ├── Client.class
│           │   │   ├── DetectLanguage.class
│           │   │   ├── Result.class
│           │   │   ├── errors
│           │   │   │   └── APIError.class
│           │   │   └── responses
│           │   │       ├── BatchDetectResponse.class
│           │   │       ├── BatchDetectionsData.class
│           │   │       ├── DetectResponse.class
│           │   │       ├── DetectionsData.class
│           │   │       ├── ErrorData.class
│           │   │       ├── ErrorResponse.class
│           │   │       ├── Response.class
│           │   │       └── StatusResponse.class
│           │   └── gtranslate
│           │       ├── Audio.class
│           │       ├── Language.class
│           │       ├── Main.class
│           │       ├── Translator.class
│           │       ├── URLCONSTANTS.class
│           │       ├── parsing
│           │       │   ├── Parse.class
│           │       │   ├── ParseTextDetect.class
│           │       │   └── ParseTextTranslate.class
│           │       ├── text
│           │       │   ├── Text.class
│           │       │   └── TextTranslate.class
│           │       └── utils
│           │           └── WebUtils.class
│           ├── javazoom
│           │   └── jl
│           │       ├── converter
│           │       │   ├── Converter$PrintWriterProgressListener.class
│           │       │   ├── Converter$ProgressListener.class
│           │       │   ├── Converter.class
│           │       │   ├── RiffFile$RiffChunkHeader.class
│           │       │   ├── RiffFile.class
│           │       │   ├── WaveFile$WaveFileSample.class
│           │       │   ├── WaveFile$WaveFormat_Chunk.class
│           │       │   ├── WaveFile$WaveFormat_ChunkData.class
│           │       │   ├── WaveFile.class
│           │       │   ├── WaveFileObuffer.class
│           │       │   ├── jlc$jlcArgs.class
│           │       │   └── jlc.class
│           │       ├── decoder
│           │       │   ├── BitReserve.class
│           │       │   ├── Bitstream.class
│           │       │   ├── BitstreamErrors.class
│           │       │   ├── BitstreamException.class
│           │       │   ├── Control.class
│           │       │   ├── Crc16.class
│           │       │   ├── Decoder$Params.class
│           │       │   ├── Decoder.class
│           │       │   ├── DecoderErrors.class
│           │       │   ├── DecoderException.class
│           │       │   ├── Equalizer$EQFunction.class
│           │       │   ├── Equalizer.class
│           │       │   ├── FrameDecoder.class
│           │       │   ├── Header.class
│           │       │   ├── InputStreamSource.class
│           │       │   ├── JavaLayerError.class
│           │       │   ├── JavaLayerErrors.class
│           │       │   ├── JavaLayerException.class
│           │       │   ├── JavaLayerHook.class
│           │       │   ├── JavaLayerUtils.class
│           │       │   ├── LayerIDecoder$Subband.class
│           │       │   ├── LayerIDecoder$SubbandLayer1.class
│           │       │   ├── LayerIDecoder$SubbandLayer1IntensityStereo.class
│           │       │   ├── LayerIDecoder$SubbandLayer1Stereo.class
│           │       │   ├── LayerIDecoder.class
│           │       │   ├── LayerIIDecoder$SubbandLayer2.class
│           │       │   ├── LayerIIDecoder$SubbandLayer2IntensityStereo.class
│           │       │   ├── LayerIIDecoder$SubbandLayer2Stereo.class
│           │       │   ├── LayerIIDecoder.class
│           │       │   ├── LayerIIIDecoder$III_side_info_t.class
│           │       │   ├── LayerIIIDecoder$SBI.class
│           │       │   ├── LayerIIIDecoder$Sftable.class
│           │       │   ├── LayerIIIDecoder$gr_info_s.class
│           │       │   ├── LayerIIIDecoder$temporaire.class
│           │       │   ├── LayerIIIDecoder$temporaire2.class
│           │       │   ├── LayerIIIDecoder.class
│           │       │   ├── Manager.class
│           │       │   ├── Obuffer.class
│           │       │   ├── OutputChannels.class
│           │       │   ├── SampleBuffer.class
│           │       │   ├── Source.class
│           │       │   ├── SynthesisFilter.class
│           │       │   ├── au2lin.ser
│           │       │   ├── huffcodetab.class
│           │       │   ├── l3reorder.ser
│           │       │   ├── lin2au.ser
│           │       │   ├── readme.txt
│           │       │   └── sfd.ser
│           │       └── player
│           │           ├── AudioDevice.class
│           │           ├── AudioDeviceBase.class
│           │           ├── AudioDeviceFactory.class
│           │           ├── FactoryRegistry.class
│           │           ├── JavaSoundAudioDevice.class
│           │           ├── JavaSoundAudioDeviceFactory.class
│           │           ├── NullAudioDevice.class
│           │           ├── Player.class
│           │           ├── PlayerApplet.class
│           │           ├── advanced
│           │           │   ├── AdvancedPlayer.class
│           │           │   ├── PlaybackEvent.class
│           │           │   ├── PlaybackListener.class
│           │           │   ├── jlap$1.class
│           │           │   ├── jlap$InfoListener.class
│           │           │   └── jlap.class
│           │           └── jlp.class
│           └── sample
│               ├── LangMap.class
│               ├── Main$1.class
│               └── Main.class
├── readme.md
├── resources
│   └── images
│       └── Transclip.svg
└── src
    ├── META-INF
    │   └── MANIFEST.MF
    ├── com
    │   ├── detectlanguage
    │   │   ├── Client.java
    │   │   ├── DetectLanguage.java
    │   │   ├── Result.java
    │   │   ├── errors
    │   │   │   └── APIError.java
    │   │   └── responses
    │   │       ├── BatchDetectResponse.java
    │   │       ├── BatchDetectionsData.java
    │   │       ├── DetectResponse.java
    │   │       ├── DetectionsData.java
    │   │       ├── ErrorData.java
    │   │       ├── ErrorResponse.java
    │   │       ├── Response.java
    │   │       └── StatusResponse.java
    │   └── gtranslate
    │       ├── Audio.java
    │       ├── Language.java
    │       ├── Main.java
    │       ├── Translator.java
    │       ├── URLCONSTANTS.java
    │       ├── parsing
    │       │   ├── Parse.java
    │       │   ├── ParseTextDetect.java
    │       │   └── ParseTextTranslate.java
    │       ├── text
    │       │   ├── Text.java
    │       │   └── TextTranslate.java
    │       └── utils
    │           └── WebUtils.java
    ├── javazoom
    │   └── jl
    │       ├── converter
    │       │   ├── Converter.java
    │       │   ├── RiffFile.java
    │       │   ├── WaveFile.java
    │       │   ├── WaveFileObuffer.java
    │       │   └── jlc.java
    │       ├── decoder
    │       │   ├── BitReserve.java
    │       │   ├── Bitstream.java
    │       │   ├── BitstreamErrors.java
    │       │   ├── BitstreamException.java
    │       │   ├── Control.java
    │       │   ├── Crc16.java
    │       │   ├── Decoder.java
    │       │   ├── DecoderErrors.java
    │       │   ├── DecoderException.java
    │       │   ├── Equalizer.java
    │       │   ├── FrameDecoder.java
    │       │   ├── Header.java
    │       │   ├── InputStreamSource.java
    │       │   ├── JavaLayerError.java
    │       │   ├── JavaLayerErrors.java
    │       │   ├── JavaLayerException.java
    │       │   ├── JavaLayerHook.java
    │       │   ├── JavaLayerUtils.java
    │       │   ├── LayerIDecoder.java
    │       │   ├── LayerIIDecoder.java
    │       │   ├── LayerIIIDecoder.java
    │       │   ├── Manager.java
    │       │   ├── Obuffer.java
    │       │   ├── OutputChannels.java
    │       │   ├── SampleBuffer.java
    │       │   ├── Source.java
    │       │   ├── SynthesisFilter.java
    │       │   ├── au2lin.ser
    │       │   ├── huffcodetab.java
    │       │   ├── l3reorder.ser
    │       │   ├── lin2au.ser
    │       │   ├── readme.txt
    │       │   └── sfd.ser
    │       └── player
    │           ├── AudioDevice.java
    │           ├── AudioDeviceBase.java
    │           ├── AudioDeviceFactory.java
    │           ├── FactoryRegistry.java
    │           ├── JavaSoundAudioDevice.java
    │           ├── JavaSoundAudioDeviceFactory.java
    │           ├── NullAudioDevice.java
    │           ├── Player.java
    │           ├── PlayerApplet.java
    │           ├── advanced
    │           │   ├── AdvancedPlayer.java
    │           │   ├── PlaybackEvent.java
    │           │   ├── PlaybackListener.java
    │           │   └── jlap.java
    │           └── jlp.java
    └── sample
        ├── LangMap.java
        └── Main.java

42 directories, 192 files
qed
  • 22,298
  • 21
  • 125
  • 196

1 Answers1

0

When you're executing your jar file from the output directory of your build, it's finding the commons-collection dependency in the same directory. When you copy the jar file elsewhere and execute there, it can no longer find the commons-collection dependency. That's why it's throwing a ClassNotFoundException. You need to copy the dependencies too, not just the jar.

You should also consider making this part of your build so that dependencies get copied to the lib folder of your build output and your manifest knows to reference the dependencies within the lib folder. If you don't want to have the manfiest updated with all required dependencies, then you'd have to specify them in the -classpath argument of the java command, which isn't ideal if the number of dependencies grows.

Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68