Java Applets: Document Base and Code

The document base is the URL of the HTML file containing this applet. The method

getDocumentBase() returns this URL.

The code base, on the other hand, is the name of the base directory from where the applet’s class file was loaded. The values of the document base and code base are the same, unless the codebase attribute of the <applet> tag is used to specify a different URL.

Typically, a code base (if specified) refers to a subdirectory of the document base directory where applet’s class files may be found. This is because browsers restrict applets from accessing arbitrary directories/URLs. If access permissions are given to the document base, subdirectories have similar permissions. So, specifying a subdirectory as the code base is always safe. Consider the following applet (, which displays the code base and document base.


public class Bases extends java.applet.Applet


public void paint(java.awt.Graphics g)


g.drawString(“Document base : ” + getDocumentBase(), 20, 10);

g.drawString(“Code base : ” + getCodeBase(), 20, 30);



Now, compile this applet and create an HTML page containing the following:

<applet code=”Bases” codebase=”./applets” width=”400″ height=”40″>


The result is shown in Figure 16.14:

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *