JDK-4456393 : (api) getDocumentBase and getCodeBase do each other's jobs.
  • Type: Bug
  • Component: docs
  • Sub-Component: guides
  • Affected Version:
    pp_1.0,1.2.1,1.3.0,1.3.1,1.3.1_03,1.4.0,1.4.1 pp_1.0,1.2.1,1.3.0,1.3.1,1.3.1_03,1.4.0,1.4.1
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    generic,solaris_7,solaris_8,windows_nt,windows_2000 generic,solaris_7,solaris_8,windows_nt,windows_2000
  • CPU: generic,x86,sparc
  • Submitted: 2001-05-09
  • Updated: 2003-02-11
  • Resolved: 2003-01-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
1.4.2 b14Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
According to the 1.3 API doc, getDocumentBase() is supposed to
return the URL of the directory of the document in which an applet
is embedded.  According to the 1.3 API doc, getCodeBase is supposed
to return the URL of the applet itself.  The actual behavior is
reversed and has been reversed for a long time.

algol% cat SimpleApplet.java
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class SimpleApplet extends Applet{
  public void init() {
  public void start() {
  public void stop() {
  public void destroy() {
     System.out.println("preparing to unload...");
  public void paint(Graphics g){
     g.drawRect(0, 0, getSize().width -1, getSize().height -1);
     g.drawString("getDocumentBase: "+getDocumentBase(), 15, 25);
     g.drawString("getCodeBase: "+getCodeBase(), 15, 35);

algol% cat SimpleApplet.html
<APPLET CODE=SimpleApplet.class WIDTH=200 HEIGHT=100>

I tested a large number of JDKs and found that the results have been stable
for a long time (at least on Solaris/sparc):

getDocumentBase  file:/home/ajacobs/public_html/BasicJava1/SimpleApplet.html
getCodeBase      file:/home/ajacobs/public_html/BasicJava1/

The JDKs checked:  1.1.6, 1.1.7, 1.1.8_13, 1.2 FCS, 1.2.1_004, 1.2.2_008,
1.2.2_08, 1.3.1, and 1.4.

Testing on Merlin (1.4) shows that behavior is consistent on Windows and

According to the JDK 1.3 API documentation, the results should have been

getCodeBase       file:/home/ajacobs/public_html/BasicJava1/SimpleApplet.html
getDocumentBase   file:/home/ajacobs/public_html/BasicJava1/

allan.jacobs@Eng 2001-05-09

Name: jk109818			Date: 11/05/2001

java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

In the API, getDocumentBase() is listed as "Return(ing) an absolute URL naming
the directory of the document in which the applet is embedded." However
getDocumentBase(), when a toString() is used prints "the URL of the applet
itself" -- the function listed for getCodeBase(). A Simalar screen print of the
toString() of getCodeBase() displays the base directory of the applet.

Please contact me if I have misinterpreted the API.
I have found this discrepancy on VM's 1.3.1 and 1.3.0.
(Review ID: 134789)

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis mantis-beta FIXED IN: mantis mantis-beta INTEGRATED IN: mantis-b14 mantis-beta

EVALUATION This is not necessarily a documentation problem. It is possibly a problem with AppletViewer's implementation of the java.applet.AppletStub interface. Does this same behaviour exist in Plug-In? iris.garcia@eng 2001-05-10 JDK 1.4 plugins are not functional. I failed to get a JDK 1.4 plugin to work in Netscape 4.75, 4.76, and Netscape 6.01A. JDK 1.3.1 plugins behave incorrectly. The behavior is different from that in appletviewer. Maybe there are two bugs. Both getDocumentBase and getCodeBase return the directory name. Java2Applet.html is attached. Referencing SimpleApplet.html is run by the JDK 1.1 JRE bundled with Netscape 4.75. allan.jacobs@Eng 2001-05-10 Here's another note from ajacobs@eng: Nice catch. The JDK 1.3.1 plugin returns http://sqesvr.eng/~ajacobs/BasicJava1/ from getDocumentBase(), and the same http://sqesvr.eng/~ajacobs/BasicJava1/ from getCodeBase(). JDK 1.3.1 appletviewer (and JDK 1.4 appletviewer) returns http://sqesvr.eng/~ajacobs/BasicJava1/SimpleApplet.html from getDocumentBase(), and http://sqesvr.eng/~ajacobs/BasicJava1/ from getCodeBase(). The documentation says that the correct values should be http://sqesvr.eng/~ajacobs/BasicJava1/ from getDocumentBase(), and http://sqesvr.eng/~ajacobs/BasicJava1/SimpleApplet.html from getCodeBase(). My attempt to get a JDK 1.4 plugin to work have so far failed. Netscape 6 core dumps. Netscape 4.75 refused to take it. This is not going to get fixed in 1.3.1. I'll leave it a Merlin bug and move it back to subcategory=appletviewer. I will try one more time to get a JDK 1.4 plugin to work in a browser. If I fail, I will get help from the plugin testers. So, it looks like Plug-In and AppletViewer are both having problems with correctly implementing the spec. Since Plug-In owns the spec, they need to decide what (if any) modifications need to be made to the API for getCodeBase() and getDocumentBase() in java.applet.Applet and java.applet.AppletStub. I designate this as the "spec" bug. Additional bugs may be necessary to track work to update AppletViewer and Plug-In to properly implement the spec. Routing the bug to jmelvin@east who owns the spec for java.applet. iris.garcia@eng 2001-05-11 Engineering has provided the following text as a fix to this doc problem (no code change): /** * Gets the URL of the document in which this applet is embedded. * For example, suppose an applet is contained * within the document: * <blockquote><pre> * http://java.sun.com/products/jdk/1.2/index.html * </pre></blockquote> * The document base is: * <blockquote><pre> * http://java.sun.com/products/jdk/1.2/index.html * </pre></blockquote> * * @return the {@link java.net.URL} of the document that contains this * applet. * @see java.applet.Applet#getCodeBase() */ public URL getDocumentBase() { return stub.getDocumentBase(); } /** * Gets the base URL. This is the URL of the directory * which contains this applet. * * @return the base {@link java.net.URL} of * the directory which contains this applet. * @see java.applet.Applet#getDocumentBase() */ public URL getCodeBase() { return stub.getCodeBase(); } ###@###.### 2002-10-23

SUGGESTED FIX Change the documentation and maintain backwards compatibility. allan.jacobs@Eng 2001-05-09

PUBLIC COMMENTS java.applet.getDocumentBase() is supposed to return the absolute URL of the directory of the document containing the applet. java.applet.getCodeBase() is supposed to return the URL of the applet itself (presumably meaning the URL of the document containing the applet). In practice, the behavior of these two methods is reversed. getDocumentBase returns the URL of the document instead of the directory and getCodeBase returns the URL of the directory. allan.jacobs@Eng 2001-05-09

WORK AROUND Ignore the documentation. Use getCodeBase to to the work of getDocumentBase. Use getDocumentBase to do the work of getCodeBase. allan.jacobs@Eng 2001-05-09