0

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

<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.10</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>2.4.3</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
            </execution>
          </executions>
        </plugin> 
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.12.4</version>
        </plugin>
      </plugins>
    </pluginManagement> 
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

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.

Sood
  • 149
  • 1
  • 1
  • 11

2 Answers2

2

You're misspelling repository in your manual classpath specification.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

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
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • I neglected to mention I'm on a Linux system Ubuntu 14.04 to be exact. I executed your command, still no change. I even extracted the jar and found the class file inside. – Sood Nov 04 '16 at 08:07
  • 1
    can you try with exact path that marking with ~ – kuhajeyan Nov 04 '16 at 08:09
  • thanks kuhajeyan it's working now. Is there anyway to skip adding this to the classpath manually? – Sood Nov 04 '16 at 08:22
  • 1
    yes, you should use assembly plugin to make fat jar with your needed dependencies, so when you execute the jar those are by default available in classpath http://maven.apache.org/plugins/maven-assembly-plugin/usage.html – kuhajeyan Nov 04 '16 at 09:12