Java NoClassDefFoundError using maven [duplicate]

Mobile Technologies Mobile Computing 3 years ago

5.54K 1 0 0 0

_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.

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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
0 views
0 shares

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.

Similar Forum


Q

Would Firebase be a good choice as a server for the Flutter mobile game I'm creating?

_x000D_ _x000D_ I am building a turn-based Flutter game. Would Firebase be a good choice as a server...
Q

Implement Bottom Navigation activity along with a VerticalViewPager with nested Cardview in Android...

_x000D_ _x000D_ I want to develop an Android App which will have a Bottom Navigation Bar and above i...
Q

(JS) For , determine if the device offers or not to capture a photo from the camera

_x000D_ _x000D_ When uploading a file, I need to give the user the possibility: upload the file fr...

Important Mobile Technologies Links