Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:55:44+00:00 2026-06-13T13:55:44+00:00

I need to encode some data in the Base64 encoding in Java. How do

  • 0

I need to encode some data in the Base64 encoding in Java. How do I do that? What is the name of the class that provides a Base64 encoder?


I tried to use the sun.misc.BASE64Encoder class, without success. I have the following line of Java 7 code:

wr.write(new sun.misc.BASE64Encoder().encode(buf));

I’m using Eclipse. Eclipse marks this line as an error. I imported the required libraries:

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

But again, both of them are shown as errors. I found a similar post here.

I used Apache Commons as the solution suggested by including:

import org.apache.commons.*;

and importing the JAR files downloaded from: http://commons.apache.org/codec/

But the problem still exists. Eclipse still shows the errors previously mentioned. What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:55:46+00:00Added an answer on June 13, 2026 at 1:55 pm

    You need to change the import of your class:

    import org.apache.commons.codec.binary.Base64;
    

    And then change your class to use the Base64 class.

    Here’s some example code:

    byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
    System.out.println("encodedBytes " + new String(encodedBytes));
    byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
    System.out.println("decodedBytes " + new String(decodedBytes));
    

    Then read why you shouldn’t use sun.* packages.


    Update (2016-12-16)

    You can now use java.util.Base64 with Java 8. First, import it as you normally do:

    import java.util.Base64;
    

    Then use the Base64 static methods as follows:

    byte[] encodedBytes = Base64.getEncoder().encode("Test".getBytes());
    System.out.println("encodedBytes " + new String(encodedBytes));
    byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
    System.out.println("decodedBytes " + new String(decodedBytes));
    

    If you directly want to encode string and get the result as encoded string, you can use this:

    String encodeBytes = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
    

    See Java documentation for Base64 for more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a StringOutputStream class in Java because I need the pre-Base64-encoded data
If you have binary data that you need to encode, what encoding scheme do
I need to send an apex web service some data. Then name of the
I receive some xml-files with embedded base64-encoded images, that I need to decode and
I get an input string with some data that's base64 encoded. Unfortunately, it gets
i need to use base 64 to encrypt some data. but while everyone can
I need to read some data from database my controller is class Pro_ajax extends
I have an app that needs to encode some data using AES/CBC/no padding. The
I need to encode/decode UTF-16 byte arrays to and from java.lang.String . The byte
I need to encode streams of 8 byte such that encoded stream has only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.