title: Encoding as Base64 in Java
body: <p>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?</p>
  
  <hr>
  
  <p>I tried to use the <code>sun.misc.BASE64Encoder</code> class, without success. I have the following line of Java 7 code:</p>
  
  <pre><code>wr.write(new sun.misc.BASE64Encoder().encode(buf));
  </code></pre>
  
  <p>I'm using Eclipse. Eclipse marks this line as an error. I imported the required libraries:</p>
  
  <pre><code>import sun.misc.BASE64Encoder;
  import sun.misc.BASE64Decoder;
  </code></pre>
  
  <p>But again, both of them are shown as errors. I found <a href="https://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-got-error-in-eclipse">a similar post here</a>.</p>
  
  <p>I used Apache Commons as the solution suggested by including:</p>
  
  <pre><code>import org.apache.commons.*;
  </code></pre>
  
  <p>and importing the JAR files downloaded from: <a href="http://commons.apache.org/codec/" rel="noreferrer">http://commons.apache.org/codec/</a></p>
  
  <p>But the problem still exists. Eclipse still shows the errors previously mentioned. What should I do?</p>