Applet Loader Demo : Applet Loader « Development Class « Java

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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w___ww._j__a__v__a__2s___.__c_o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.