java.lang.LinkageError exception when you load an applet (294878)



The information in this article applies to:

  • Microsoft virtual machine

This article was previously published under Q294878

SYMPTOMS

When you load an HTML page that contains a Java applet, you may receive a java.lang.LinkageError exception. If the Java console is enabled in Microsoft Internet Explorer, the Java console displays output similar to the following:
Error loading class: com/myCompany/TestApplet
java.lang.LinkageError: Class already defined in the context of this loader: 
com/myCompany/TestApplet
java.lang.ClassNotFoundException: com/myCompany/TestApplet
	at com/ms/vm/loader/URLClassLoader.loadClass
	at com/ms/vm/loader/URLClassLoader.loadClass
	at com/ms/applet/AppletPanel.securedClassLoad
	at com/ms/applet/AppletPanel.processSentEvent
	at com/ms/applet/AppletPanel.processSentEvent
	at com/ms/applet/AppletPanel.run
	at java/lang/Thread.run
					

CAUSE

This exception occurs because the full Java class package name uses a forward slash (/) instead of a period (.), and the package name does not explicitly add the .class extension to the end of the Java class. For example:
<APPLET code=com/myCompany/TestApplet
	width=200
	height=25 VIEWASTEXT>
</APPLET> 
				

RESOLUTION

To resolve this problem, use one of the three working code class path names that are shown in the "More Information" section.

MORE INFORMATION

Use this simple Java applet to demonstrate the LinkageError exception, as well as to show appropriate applet full package names:
package com.myCompany;

public class TestApplet extends java.applet.Applet
{
	public void init() {
		this.setBackground(java.awt.Color.red);
	}
}
				
Place this applet code in a directory, and build it. This creates a com\myCompany subdirectory that contains the TestApplet.class file.

In the same root project directory, create the following HTML file. This code contains four <APPLET> tags that attempt to load the class with various code paths:
<HTML>
<BODY>

code=com.myCompany.TestApplet.class <BR>
<APPLET code=com.myCompany.TestApplet.class
	width=200
	height=25 VIEWASTEXT>
</APPLET> <BR>

code=com.myCompany.TestApplet <BR>
<APPLET code=com.myCompany.TestApplet
	width=200
	height=25 VIEWASTEXT>
</APPLET> <BR>

code=com/myCompany/TestApplet.class <BR>
<APPLET code=com/myCompany/TestApplet.class
	width=200
	height=25 VIEWASTEXT>
</APPLET> <BR><BR>

<B>This pathname will generate the LinkageError: <BR>
code=com/myCompany/TestApplet </B><BR>
<APPLET code=com/myCompany/TestApplet
	width=200
	height=25 VIEWASTEXT>
</APPLET> <BR>

</BODY>
</HTML>
				
The first three examples show valid class names with the full package name specified. However, the last example generates the LinkageError exception because it uses "/" package delimiters and does not include the .class extension at the end of the class name.

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbJava kbprb KB294878