-2

I'm making a project to modify a .wav file to .flac file, I use the class http://javaflacencoder.sourceforge.net/javadoc/index.html. My project in IDE eclipse works well. But when I exported it into .jar and run it with command line on linux. There is the error:

 Exception in thread "main" java.lang.NoClassDefFoundError: javaFlacEncoder/FLAC_FileEncoder
    at speech.Speech.main(Speech.java:44)
Caused by: java.lang.ClassNotFoundException: javaFlacEncoder.FLAC_FileEncoder
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 1 more

Without idea, I can't understand why. Could anybody have the same problem? Could you show me how to resolve it? Thanks,

VUONG Tam
  • 21
  • 4
  • Whats the command you're typing on terminal? Does your project reference any external libs? Try adding -cp PATH of your libs. – AndreDuarte Sep 12 '14 at 13:08
  • Did you export the external lib when building the jar ? – ortis Sep 12 '14 at 13:08
  • Did you provide proper classpath for executing jar? – Alex Sep 12 '14 at 13:09
  • 1
    look at the FAQ http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java – Martin Frank Sep 12 '14 at 13:10
  • @AndreDuarte: I use this command line: java -jar project.jar Audio.wav. When I created the project, I have added some externel libraries. Could you show more clearly how to add -cp PATH in my libs? Sorry for this basic question, I'm beginning on linux :) – VUONG Tam Sep 12 '14 at 13:17

2 Answers2

0

Eclipse takes care of setting up the classpath for the execution of your Java program; you probably failed to replicate this on the command line. There are several options:

  1. if starting with java <main_class_name>, then add the -cp argument listing all the dependency JARs;

  2. if starting with java -jar <jar_name>, then make sure the JAR's manifest (MANIFEST.MF) includes the Class-Path entry, which lists the other JARs it depends on;

  3. create an "uberjar" which contains all the necessary classes. Maven's shade plugin can do that for you.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

You should probably add a classpath entry to let Java know of your librarie.

Typically, you must do:

# generate the classpath entry
declare classpath=$(for jar in lib/*.jar; do echo ":$jar"; done)
# remove the first character (':').
classpath="${classpath:1}"
java -cp "${classpath}" -jar yourjar.jar

Or:

# the same code that generate classpath
export CLASSPATH="${classpath}"
java -jar yourjar.jar

Also, in the manifest of the jar, you can list them the Class-Path entry.

NoDataFound
  • 11,381
  • 33
  • 59