Applet Loader Demo : Applet Loader « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » Applet LoaderScreenshots 
Applet Loader Demo
 
import java.applet.Applet;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;

import javax.swing.JFrame;

public class MainClass {

  public static void main(String args[]) {
    String name = "http://urlWithClassName";
    try {
      if (!name.endsWith(".class")) {
        System.err.println("That doesn't look like a byte code file!");
        return;
      }
      URL u = new URL(name);
      URLClassLoader ucl = new URLClassLoader(u);

      // parse out the name of the class from the URL
      String s = u.getFile();
      String classname = s.substring(s.lastIndexOf('/'), s.lastIndexOf(".class"));
      Class AppletClass = ucl.loadClass(classname, true);
      Applet apl = (AppletAppletClass.newInstance();
      JFrame f = new JFrame();
      f.setSize(200200);
      f.add("Center", apl);
      apl.init();
      apl.start();
      f.setVisible(true);
    catch (Exception e) {
      System.err.println(e);
    }
  }
}

class URLClassLoader extends ClassLoader {
  Hashtable cache = new Hashtable();
  URL url;

  public URLClassLoader(URL u) {
    this.url = u;
  }

  public synchronized Class loadClass(String name, boolean resolvethrows ClassNotFoundException {
    Class cls = (Classcache.get(name);
    if (cls == null) {
      try {
        cls = findSystemClass(name);
      catch (ClassNotFoundException e) {
      }
    }
    if (cls == null) {
      byte classData[] = loadClassData(name);
      cls = defineClass(classData, 0, classData.length);
      cache.put(name, cls);
    }
    if (resolve) {
      resolveClass(cls);
    }
    return cls;
  }
  private byte[] loadClassData(String namethrows ClassNotFoundException {
    byte[] buffer;
    InputStream theClassInputStream = null;
    int bufferLength = 128;
    try {
      URL classURL = new URL(url, name + ".class");
      URLConnection uc = classURL.openConnection();
      uc.setAllowUserInteraction(false);

      try {
        theClassInputStream = uc.getInputStream();
      catch (NullPointerException e) {
        System.err.println(e);
        throw new ClassNotFoundException(name + " input stream problem");
      }
      int contentLength = uc.getContentLength();

      // A lot of web servers don't send content-lengths
      // for .class files
      if (contentLength == -1) {
        buffer = new byte[bufferLength * 16];
      else {
        buffer = new byte[contentLength];
      }

      int bytesRead = 0;
      int offset = 0;

      while (bytesRead >= 0) {
        bytesRead = theClassInputStream.read(buffer, offset, bufferLength);
        if (bytesRead == -1)
          break;
        offset += bytesRead;
        if (contentLength == -&& offset == buffer.length) { // grow the array
          byte temp[] new byte[offset * 2];
          System.arraycopy(buffer, 0, temp, 0, offset);
          buffer = temp;
        else if (offset > buffer.length) {
          throw new ClassNotFoundException(name + " error reading data into the array");
        }
      }

      if (offset < buffer.length) { // shrink the array
        byte temp[] new byte[offset];
        System.arraycopy(buffer, 0, temp, 0, offset);
        buffer = temp;
      }

      // Make sure all the bytes were received
      if (contentLength != -&& offset != contentLength) {
        throw new ClassNotFoundException("Only " + offset + " bytes received for " + name
            "\n Expected " + contentLength + " bytes");
      }
    catch (Exception e) {
      throw new ClassNotFoundException(name + " " + e);
    finally {
      try {
        if (theClassInputStream != null)
          theClassInputStream.close();
      catch (IOException e) {
      }
    }
    return buffer;
  }
}

           
         
  
Related examples in the same category
1.Dummy Applet Context
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.