arsenal
arsenal

Reputation: 24194

Base64Encoder cannot be resolved

This is my Java code in a JSP file. I am getting

Base64Encoder cannot be resolved.

Why is it so? I have to add something related to Base64Encoder. Any suggestions will be appreciated.

    <%@ page language="java" import="java.io.OutputStream,java.net.HttpURLConnection,java.net.URL,java.util.Collection,org.apache.commons.httpclient.Credentials,org.apache.commons.httpclient.auth.AuthenticationException,org.apache.commons.httpclient.auth.MalformedChallengeException,org.apache.commons.httpclient.params.DefaultHttpParams,org.apache.commons.httpclient.params.HttpParams,org.apache.commons.httpclient.auth.AuthScheme,org.apache.commons.httpclient.auth.AuthPolicy,org.apache.commons.httpclient.HttpClient,org.apache.commons.httpclient.UsernamePasswordCredentials,org.apache.commons.httpclient.auth.AuthScope,org.apache.commons.httpclient.methods.GetMethod,org.w3c.dom.*,javax.xml.parsers.DocumentBuilder,javax.xml.parsers.DocumentBuilderFactory,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%
String a_Url = request.getParameter( "url" ) ;

URL url = new URL (a_Url);
String encoding = Base64Encoder.encode ("test:test");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in   = 
    new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}

%>

Upvotes: 13

Views: 73493

Answers (5)

walker
walker

Reputation: 199

"Window" -> "Preferences" -> "Java" -> "Installed JREs" change : jre installation path

It should work. enter image description here

Upvotes: 0

Greg Chappell
Greg Chappell

Reputation: 131

I suspect you're not using a standalone JRE instead of the one included in the JDK.

  1. Right click your project and click "Build Path" -> "Configure Build Path"
  2. Under "Libraries", click on the existing JRE and then click "Remove"
  3. Click "Add Library" -> "JRE System Library" -> "Finish"

The class should now resolve.

Upvotes: 13

Sai
Sai

Reputation: 3967

Looks like you are using a class that does not exist in a jar you have included in the web application. Can you try the following? Make adjustments if necessary, I am just looking at the documentation for commons and typing this out --

  1. Go to http://commons.apache.org/codec/index.html and read through the information there
  2. Now go to http://commons.apache.org/codec/download_codec.cgi and download the zip file
  3. Extract out the jar file and copy it to the lib directory of your web application
  4. Replace the line [String encoding = Base64Encoder.encode ("test:test");]

with

String encoding = new String(
 org.apache.commons.codec.binary.Base64.encodeBase64   
    (org.apache.commons.codec.binary.StringUtils.getBytesUtf8("test:test"))
  );

Upvotes: 16

Kristofer Hoch
Kristofer Hoch

Reputation: 246

I don't see an inclusion of the namespace here for Base64Encoder. Try adding 'com.oreilly.servlet' to your import.

Upvotes: 1

Sai
Sai

Reputation: 3967

You may need to do an import or specify the fully qualified class name for Base64Encoder

Upvotes: 2

Related Questions