JDK-4274668 : getCodeBase() should return a JAR URL if the applet's class file is in a JAR
  • Type: Bug
  • Component: deploy
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1999-09-22
  • Updated: 2017-10-06
  • Resolved: 2017-07-27
Related Reports
Relates :  
Relates :  
Description
Though not entirely clear from the spec for Applet.getCodeBase() and 
AppletContext.getCodeBase(), it seems reasonable to assume that if the class
file for an applet is within a JAR file, then the returned code base should
reference that location.  Currently it does not.

FILE: GetCodeBase.java

import java.applet.Applet;

public class GetCodeBase extends Applet {
    public void init() {
        System.out.println("getCodeBase() = " + getCodeBase());
    }
}

FILE: GetCodeBase.html

<html>
<body>

<applet width=100 height=100 code=GetCodeBase.class archive=GetCodeBase.jar>
</applet>

</body>
</html>

OUTPUT:

(iag@ribbit) $ java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-G)
Java HotSpot (TM) Client VM (build 1.3-G, interpreted mode)
(iag@ribbit) $ javac GetCodeBase.java
(iag@ribbit) $ jar cf GetCodeBase.jar GetCodeBase.class 
(iag@ribbit) $ rm GetCodeBase.class
(iag@ribbit) $ appletviewer  GetCodeBase.html
getCodeBase() = file:/home/iag/work/bug/av/

iris.garcia@eng 1999-09-22

Comments
getCodeBase returns the URL from which the resources are downloaded - be they jars or class files. - closing as not a bug
27-07-2017

EVALUATION JAR URLs were introduced in JDK1.2 this unexpected behaviour is probably an oversight. This problem arises due to a lack of clarity in the spec. To fully address this problem, The API for Applet.getCodeBase() and AppletContext.getCodeBase() should be extended to include examples of the expected return value for cases where the .class file does and does not reside in a JAR file. Intended use and past behaviour in AppletViewer and Plug-in should be considered. It seems reasonable to assume that getCodeBase() should return a JAR URL if the applet class file is contained within a JAR. A benefit of this interpretation is that getCodeBase() could be used in construting valid URLs for locating images and audio clips within the JAR. (See bug 4214785). In AppletViewer, composition of the return value for getCodeBase() is localized to the constructor for sun.applet.AppletViewerPanel (AVPanel). Currently the AVPanel is immutable and is created before the location of the applet's classfile is known. Changing this behaviour is potentially risky because of unknown dependencies on the currently stored value. For instance, the constructor for AppletClassLoader takes a single parameter, the "applet code base URL". This URL is used by AppletClassLoader.findClass() as the second search location when the class file is not found in they loaded JARs. Perhaps what should be used in this case is the value returned by Applet.getDocumentBase()? In any event, careful testing should be done when this problem is fixed. iris.garcia@eng 1999-09-22 I agree that the javadoc for Applet.getCodeBase() is inadequate and needs to be updated. This method should return the URL from the codebase field within the applet tag of an html page. If that field is not present then the URL of the document is the codebase. This is a distinct concept from the jar file which contains the main applet class. The codebase is the base URL which the various jars from the archive field in the applet tag are resolved relative to. It is also the place of last resort where individual .class files are loaded from if they are not present in any jar archives. It would be inconsistent with past behavior and "wrong" to have getCodeBase() return the jar: URL of the jar file containing the main applet class. I would suggest closing this as "not a bug". The javadoc for getCodeBase() should probably be improved but perhaps under a different bugid. jeff.nisewanger@Eng 2001-03-23
23-03-2001