Java NoClassDefFoundError using maven [duplicate]

Mobile Technologies Mobile Computing 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ Including all the jars in a directory within the Java classpath_x000D_ _x000D_ 24 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ I get a Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Hex when i execute my code. I have looked through a number of examples on this exception on stack overflow but nothing seems to help. My pom.xml looks like this commons-codec commons-codec 1.10 junit junit 3.8.1 test org.apache.maven.plugins maven-shade-plugin 2.4.3 package shade org.apache.maven.plugins maven-compiler-plugin 3.0 org.apache.maven.plugins maven-surefire-plugin 2.12.4 maven-compiler-plugin 1.8 1.8 Code to reproduce it looks like this package com.mypackage; import java.util.*; import java.io.*; import java.security.*; import org.apache.commons.codec.binary.Hex; public class Test{ private byte[] m_key; private static String resourceFolder = System.getenv("SEABED_HOME").toString() + "/testing" + "/resources"; public static void main(String[] args) { Test t = new Test(); t.readKey(); } public byte[] generateKey() { SecureRandom scr = new SecureRandom(); byte[] key = new byte[32]; scr.nextBytes(key); return key; } private void readKey() { BufferedReader br = null; try{ br = new BufferedReader(new FileReader(resourceFolder + "/.seabedkey")); m_key = Hex.decodeHex(br.readLine().toCharArray()); } catch(Exception e) { if(e instanceof FileNotFoundException) { System.out.println("Key not found, generating now"); writeKey(generateKey()); readKey(); return; } e.printStackTrace(); } finally { if(br != null) { try { br.close(); } catch(IOException e) { e.printStackTrace(); } } } } private void writeKey(byte[] key) { try{ FileOutputStream fos = new FileOutputStream(resourceFolder + "/.seabedkey"); fos.write(key); fos.close(); } catch(Exception e) { e.printStackTrace(); } } } I execute it like this java -cp target/mypackage-1.0-SNAPSHOT.jar:~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar com.mypackage.Test It compiles but then at runtime it looks like the class cannot be found. I tried changing the scope of the dependency to provided but apparently that is not the right way to use it so I switched back to the default which is compile. I can find the jar in my .m2 folder so I am sure it is there.

Posted on 16 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Probably the separator, ; for windows : for Unix if you are running in windows java -cp "target/mypackage-1.0-SNAPSHOT.jar;~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar" com.mypackage.Test Linux java -cp "target/mypackage-1.0-SNAPSHOT.jar:~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar" com.mypackage.Test

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Mobile Technologies Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community