Reputation: 4418
I am trying to use Base64
class in JDK 7 but I am getting the error:
Base64 cannot be resolved
Why does eclipse throw this error?
I am using the code below:
byte[] imageData = Base64.getDecoder().decode(readFile(imagePart.getInputStream()));
even import statement also shows the same error : import java.util.Base64;
Is this class not available in JDK 7?
Upvotes: 6
Views: 40108
Reputation: 1421
Base64.getDecoder().decode()
is available from Java 1.8
Try to use Google Guava.
pom.xml
<dependency>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
<type>jar</type>
<version>14.0.1</version>
</dependency>
Code Snippet
String inputContent = "Hello World";
String base64String = BaseEncoding.base64().encode(inputContent.getBytes("UTF-8"));
//decode
System.out.println("Base64:" + base64String);
byte[] contentInBytes = BaseEncoding.base64().decode(base64String);
System.out.println("Source content: " + new String(contentInBytes, "UTF-8"));//Hello World
Upvotes: 6
Reputation: 43788
From the documentation:
Since: 1.8
So no, it is not available in JDK 7.
Upvotes: 9
Reputation: 108
As stated java.util.Base64 is not available until 8.
However I find it worth noting that Android developers have access to android.util.Base64 at language level 7 (API 24) and the code is entirely self contained so you can copy and drop it into your project if you need Base64 and don't want to use a 3rd party like Apache. Just watch rights and usage and all that.
Upvotes: 0
Reputation: 444
If it is needed to specifically use JDK7 for your project, and you still need to use java.util.Base64 class, you can include in your project the code for that class from OpenJDK.
Source for this class is available at:
http://www.grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java?av=f
Base64.java file can be downloaded at:
http://www.grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java/?v=source&disposition=attachment
Upvotes: 6
Reputation: 2153
java.util.Base64 is available in Java 8 or better
In Java 7 you can use Apache Commons Codec
See here for examples http://www.rgagnon.com/javadetails/java-0598.html
Upvotes: 1