Skip to Content

Get Applet Directory URL or Code Base Example

  1. /*
  2.         Get Applet Directory URL or Code Base Example
  3.         This java example shows how get the current applet's directory URL or
  4.         code base using getCodeBase() method of Java Applet class.
  5. */
  6.  
  7.  
  8.  
  9. import java.applet.Applet;
  10. import java.awt.Graphics;
  11. import java.net.URL;
  12.  
  13. /*
  14. <applet code="GetCodeBaseExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class GetCodeBaseExample extends Applet{
  19.  
  20.         public void paint(Graphics g){
  21.                
  22.                 /*
  23.                  * To get Applet directory URL or the code base use
  24.                  * String getCodeBase()
  25.                  * method of Java Applet class.
  26.                  */
  27.                
  28.                 URL appletDir = getCodeBase();
  29.                 g.drawString(appletDir.toString(), 50, 50);
  30.                
  31.         }
  32. }

Example Output

Advertisement: