NoClassFoundError:org.apache.commons.codec.binary / Base64运行时异常

时间:2016-03-13 23:08:49

标签: java apache classpath

我不确定为什么这个项目会编译,但会抛出这个运行时错误。

E:\Java Projects\KServer>java Server 8096
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
        at CryptoUtil.encrypt(CryptoUtil.java:60)
        at KClient.send(KClient.java:124)
        at KClient.verify(KClient.java:176)
        at KClient.openConn(KClient.java:115)
        at Server.main(Server.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 5 more

在线它说我需要将编解码器jar添加到我的类路径中,但我想我做到了。我在项目目录中有它,我用

编译

javac -cp .;commons-codec-1.10.jar

所以我认为它确实被添加到我的课程路径中。

修改

编译命令行:

javac -cp .;commons-codec-1.10.jar Server.java KServer.java AuthProtocol.java Connection.java CryptoUtil.java KClient.java KeyGen.java UDPResponder.java

运行

java Server 8096

 Directory of E:\Java Projects\KServer

03/13/2016  06:17 PM    <DIR>          .
03/13/2016  06:17 PM    <DIR>          ..
03/13/2016  06:55 PM             1,518 AuthProtocol.class
03/02/2016  11:39 PM               956 AuthProtocol.java
03/13/2016  06:17 PM           284,184 commons-codec-1.10.jar
03/13/2016  06:55 PM             2,593 Connection.class
03/08/2016  12:25 AM             2,192 Connection.java
03/13/2016  06:55 PM             2,184 CryptoUtil.class
03/13/2016  06:43 PM             3,706 CryptoUtil.java
03/13/2016  06:55 PM             4,460 KClient.class
03/08/2016  12:25 AM             3,908 KClient.java
03/13/2016  06:55 PM               492 KeyGen.class
03/02/2016  11:12 PM               236 KeyGen.java
03/13/2016  06:55 PM               700 KServer.class
03/01/2016  09:33 PM               573 KServer.java
03/13/2016  06:55 PM               980 Server.class
03/08/2016  12:03 AM               628 Server.java
03/13/2016  06:55 PM             1,257 UDPResponder.class
03/07/2016  08:52 PM             1,007 UDPResponder.java

CrpytoUtil.java

我在这里使用了一个SO答案,这个家伙Vikram使用太阳系发布了一个解决方案&#39; base64编码API。我试图让它与apache.commons一起工作

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.codec.binary.Base64;

/*** Encryption and Decryption of String data; PBE(Password Based Encryption and Decryption)
* @author Vikram
* Modified by Me suckas.  
*/
public class CryptoUtil 
{
    Cipher ecipher;
    Cipher dcipher;
    // 8-byte Salt
    byte[] salt = {
        (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
        (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
    };
    // Iteration count
    int iterationCount = 19;
    public CryptoUtil(byte[] nacl) { 
        salt = nacl;
    }

    /**
     * 
     * @param secretKey Key used to encrypt data
     * @param plainText Text input to be encrypted
     * @return Returns encrypted text
     * 
     */
    public String encrypt(String secretKey, String plainText) 
            throws NoSuchAlgorithmException, 
            InvalidKeySpecException, 
            NoSuchPaddingException, 
            InvalidKeyException,
            InvalidAlgorithmParameterException, 
            UnsupportedEncodingException, 
            IllegalBlockSizeException, 
            BadPaddingException{
        //Key generation for enc and desc
        KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);        
         // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

        //Enc process
        ecipher = Cipher.getInstance(key.getAlgorithm());
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);      
        String charSet="UTF-8";       
        byte[] in = plainText.getBytes(charSet);
        byte[] out = ecipher.doFinal(in);
        String encStr = Base64.encodeBase64String(out);

        return encStr;
    }
     /**     
     * @param secretKey Key used to decrypt data
     * @param encryptedText encrypted text input to decrypt
     * @return Returns plain text after decryption
     */
    public String decrypt(String secretKey, String encryptedText)
     throws NoSuchAlgorithmException, 
            InvalidKeySpecException, 
            NoSuchPaddingException, 
            InvalidKeyException,
            InvalidAlgorithmParameterException, 
            UnsupportedEncodingException, 
            IllegalBlockSizeException, 
            BadPaddingException, 
            IOException{
         //Key generation for enc and desc
        KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);        
         // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
        //Decryption process; same key will be used for decr
        dcipher=Cipher.getInstance(key.getAlgorithm());
        dcipher.init(Cipher.DECRYPT_MODE, key,paramSpec);
        byte[] enc = Base64.decodeBase64(encryptedText);
        byte[] utf8 = dcipher.doFinal(enc);
        String charSet="UTF-8";     
        String plainStr = new String(utf8, charSet);
        return plainStr;
    }    
}

1 个答案:

答案 0 :(得分:1)

尝试:注意-cp适用于javac和java命令。

java -cp .;commons-codec-1.10.jar Server 8096