1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* Get Applet Directory URL or Code Base Example This java example shows how get the current applet's directory URL or code base using getCodeBase() method of Java Applet class. */ import java.applet.Applet; import java.awt.Graphics; import java.net.URL; /* <applet code="GetCodeBaseExample" width=200 height=200> </applet> */ public class GetCodeBaseExample extends Applet{ public void paint(Graphics g){ /* * To get Applet directory URL or the code base use * String getCodeBase() * method of Java Applet class. */ URL appletDir = getCodeBase(); g.drawString(appletDir.toString(), 50, 50); } } |
Example Output
As of Java 7u25, this will crash. The Applet.getCodeBase() function now returns null if the applet was loaded from a local file (see the 7u25 release notes for more).