为什么base64类没有找到代码编译好,并设置类路径

为什么下面的代码给出错误

C: temp> java test -cp commons-codec-1.9.jar auth string:xxx线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / commons / co dec / binary / Base64 at Test.main(Test .java:22)导致:java.lang.ClassNotFoundException:org.apache.commons.codec.binary.Bas在java.net.URLClassLoader上的e64 $ 1.run(URLClassLoader.java:366)在java.net.URLClassLoader $ 1。 java.net.URLClassLoader.findClass(URLClassLoader.java:354)在java.lang.ClassLoader.loadClass(ClassLoader.java:425)处运行(URLClassLoader.java:355)在java.security.AccessController.doPrivileged(Native Method) sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)at java.lang.ClassLoader.loadClass(ClassLoader.java:358)... 1 more

import org.apache.commons.codec.binary.Base64;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class Test {

    public static void main(String[] args) {

        try {
            String webPage = "xx";
            String name = "xxx";
            String password = "xx";

            String authString = name + ":" + password;
            System.out.println("auth string: " + authString);
            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
            String authStringEnc = new String(authEncBytes);
            System.out.println("Base64 encoded auth string: " + authStringEnc);

            URL url = new URL(webPage);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int numCharsRead;
            char[] charArray = new char[1024];
            StringBuffer sb = new StringBuffer();
            while ((numCharsRead = isr.read(charArray)) > 0) {
                sb.append(charArray, 0, numCharsRead);
            }
            String result = sb.toString();

            System.out.println("*** BEGIN ***");
            System.out.println(result);
            System.out.println("*** END ***");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

尝试在类名之前的命令中放置cp标志:

java -cp commons-codec-1.9.jar Test

这是来自java命令:

用法:java [-options] class [args ...]

(执行一个类)

链接地址: http://www.djcxy.com/p/20265.html

上一篇: why base64 class is not found when code compiles ok and classpath is set

下一篇: Java Picasa API Directory listing not working