简体   繁体   中英

Package com.sun.org.apache.xml.internal.security.utils.Base64 does not exist

I am using NetBeans 7.0.1 and JDK 1.6 Update 24 and when importing the package com.sun.org.apache.xml.internal.security.utils.Base64 to encode a password hash:

u.setPassword(Base64.encode(digest(password)));

When compiling however, I get the following error:

(omitted)\RegistrationController.java:8: package com.sun.org.apache.xml.internal.security.utils does not exist
import com.sun.org.apache.xml.internal.security.utils.Base64;

(omitted)\RegistrationController.java:94: cannot find symbol
symbol  : variable Base64
location: class RegistrationController
    u.setPassword(Base64.encode(digest(password)));
2 errors

I have read several other questions regarding the same problem but all deal with packages that are not part of the JDK whereas this is ( rt.jar ). Code Assists works on the type and expanding the jar reveals both the source code and binary are in fact present as well.

I have tried cleaning the project, rebuilding it, copying the source files over to a brand new Enterprise project and running NetBeans as Administrator, all to no avail. Any clue on how to solve this very much appreciated!

Thanks!

com.sun.* classes are not part of the Java API, and you shouldn't be relying on them. I would suggest using Apache Commons Codec to do Base64 encoding instead.

As Greg pointed out in a comment to the previously accepted answer :

Note from the future: in Java 8, there is a java.util.Base64 package.

Since it is now implemented as a part of the standard Java API, it is probably preferable to use it instead of Apache Commons Codec. With this in mind I think this should be posted as an answer.

The API has a number of methods in the class Base64 to create a Decoder or Encoder . The following types of encoders and decoders are supported:

  • Basic - Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for encoding and decoding operation. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.

  • URL and Filename safe - Uses the "URL and Filename safe Base64 Alphabet" as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.

  • MIME - Uses the "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for encoding and decoding operation. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\\r' followed immediately by a linefeed '\\n' as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.

So, for example, this is how to create a basic encoder and encode a byte array:

byte[] encodedArray = Base64.getEncoder().encode(someArrayToEncode);

使用org.apache.commons.codec.binary.Base64使用特定方法(例如Base64.encodeBase64String() )替换Base64.encode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM