我得到一个
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Hex
当我执行我的代码时。
我已经查看了许多有关堆栈溢出异常的示例,但似乎没有任何帮助。我的 pom.xml 看起来像这样
<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>
重现它的代码如下所示
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();
}
}
}
我是这样执行的
java -cp target/mypackage-1.0-SNAPSHOT.jar:~/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar com.mypackage.Test
它可以编译,但在运行时似乎找不到该类。我尝试将依赖项的范围更改为 provided
但显然这不是正确的使用方法,所以我切换回默认值 compile
。我可以在 .m2 文件夹中找到该 jar,因此我确定它在那里。
最佳答案
您在手动类路径规范中拼写错误了 repository
。
关于使用maven的Java NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415765/