我得到了
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,所以我确信它在那里。
答案 0 :(得分:2)
您在手动类路径规范中错误拼写repository
。
答案 1 :(得分:1)
可能是分隔符,;对于Windows:对于Unix
如果您在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