IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Applets Java Discussion :

Erreur à l'exécution d'une applet java à partir d'une page web


Sujet :

Applets Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 15
    Points : 7
    Points
    7
    Par défaut Erreur à l'exécution d'une applet java à partir d'une page web
    Bonjour,
    lors de l'exécution d'une applet java avec un navigateur web le navigateur affiche l'erreur suivant:
    java.lang.NoClassDefFoundError: SweetHome3DApplet (wrong name: com/eteks/sweethome3d/applet/SweetHome3DApplet)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception : java.lang.NoClassDefFoundError: SweetHome3DApplet (wrong name: com/eteks/sweethome3d/applet/SweetHome3DApplet)
    par contre, quand je l'exécute sur eclipse avec AppletViewer j'ai aucun problème.
    j'ai mis la page html ds le même dossier ou se trouve la classe que j'appelle dans cette page html (SweetHome3DApplet.class).
    voila le chemin de ce dossier:
    D:\eclipse\Workspace\SweetHome3D\classes\com\eteks\sweethome3d\applet

    et voila le code source de l'applet java:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
     
    package com.eteks.sweethome3d.applet;
     
    import java.io.IOException;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.security.AccessControlException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
     
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
     
    import com.eteks.sweethome3d.tools.ExtensionsClassLoader;
     
      public class SweetHome3DApplet extends JApplet {
     
     
      private static final long serialVersionUID = 1L;
      private Object appletApplication;
     
     
     
      @Override
      public void start() {
        // TODO Auto-generated method stub
        super.start();
      }
     
      @Override
      public void stop() {
        // TODO Auto-generated method stub
        super.stop();
      }
     
      @Override
      public void init() {
     
      System.out.println("simo init");
     
        if (!isJava5OrSuperior()) {
          showError("<html><p>This applet may be run under Windows, Mac OS X 10.4 / 10.5, Linux and Solaris." +
              "<br>It requires Java version 5 or superior.</p>" +
              "<p>Please, check Java version set in Java preferences under Mac OS X," +
              "<br>or update your Java Runtime to the latest version available at java.com under the other systems.</p>");
        } else {
          createAppletApplication();
        }
     
      }
     
      public void destroy() {
        if (this.appletApplication != null) {
          try {
            Method destroyMethod = this.appletApplication.getClass().getMethod("destroy", new Class [0]);
            destroyMethod.invoke(this.appletApplication, new Object [0]);
          } catch (Exception ex) {
            // Can't do better than print stack trace when applet is destroyed
            ex.printStackTrace();
          }
        }
        this.appletApplication = null;
        // Collect deleted objects (seems to be required under Mac OS X when the applet is being reloaded)
        System.gc();
      }
     
      /**
       * Returns <code>true</code> if one of the homes edited by this applet is modified. 
       */
      public boolean isModified() {
        if (this.appletApplication != null) {
          try {
            Method destroyMethod = this.appletApplication.getClass().getMethod("isModified", new Class [0]);
            return ((Boolean)destroyMethod.invoke(this.appletApplication, new Object [0])).booleanValue();
          } catch (Exception ex) {
            // Can't do better than print stack trace
            ex.printStackTrace();
          }
        }
        return false;
      }
     
      /**
       * Returns <code>true</code> if current JVM version is 5+. 
       */
     
     
      private boolean isJava5OrSuperior() {
        String javaVersion = System.getProperty("java.version");
        String [] javaVersionParts = javaVersion.split("\\.|_");
        if (javaVersionParts.length >= 1) {
          try {
            // Return true for Java SE 5 and superior
            if (Integer.parseInt(javaVersionParts [1]) >= 5) {
              return true;
            }
          } catch (NumberFormatException ex) {
          }
        }
        return false;
      }
     
      /**
       * Shows the given text in a label.
       */
     
     
      private void showError(String text) {
        JLabel label = new JLabel(text, JLabel.CENTER);
        setContentPane(label);
      }
     
      /**
       * Creates a new <code>AppletApplication</code> instance that manages this applet content.
       */
     
     
      private void createAppletApplication() {
    System.out.println("simo createAppletApplication");
     
        try {
          Class sweetHome3DAppletClass = SweetHome3DApplet.class;
          List java3DFiles = new ArrayList(Arrays.asList(new String [] {
              "j3dcore.jar", // Main Java 3D jars
              "vecmath.jar",
              "j3dutils.jar",
              "windows/j3dcore-d3d.dll", // Windows DLLs
              "windows/j3dcore-ogl.dll",
              "windows/j3dcore-ogl-cg.dll",
              "windows/j3dcore-ogl-chk.dll",
              "macosx/gluegen-rt.jar", // Mac OS X jars and DLLs
              "macosx/jogl.jar",
              "macosx/libgluegen-rt.jnilib",
              "macosx/libjogl.jnilib",
              "macosx/libjogl_awt.jnilib",
              "macosx/libjogl_cg.jnilib"}));
          if (System.getProperty("os.name").startsWith("Linux")
              && "64".equals(System.getProperty("sun.arch.data.model"))) {
            java3DFiles.add("linux/x64/libj3dcore-ogl.so"); // Linux DLLs
          } else {
            java3DFiles.add("linux/i386/libj3dcore-ogl.so"); 
            java3DFiles.add("linux/i386/libj3dcore-ogl-cg.so");
          }
          List applicationPackages = new ArrayList(Arrays.asList(new String [] {
              "com.eteks.sweethome3d",
              "javax.media",
              "javax.vecmath",
              "com.sun.j3d",
              "com.sun.opengl",
              "com.sun.gluegen.runtime",
              "javax.media.opengl",
              "com.sun.media",
              "com.ibm.media",
              "jmpapps.util",
              "com.microcrowd.loader.java3d",
              "org.sunflow"}));
          applicationPackages.addAll(getPluginsPackages());
     
          String applicationClassName = getApplicationClassName();
          if (!applicationClassName.startsWith((String)applicationPackages.get(0))) {
            String [] applicationClassParts = applicationClassName.split("\\.");
            String applicationClassPackageBase = ""; 
            // Contains the two first part of class package at most
            for (int i = 0, n = Math.min(applicationClassParts.length - 1, 2); i < n; i++) {
              if (i > 0) {
                applicationClassPackageBase += ".";
              }
              applicationClassPackageBase += applicationClassParts [i];
            }
            applicationPackages.add(applicationClassPackageBase);
          }
     
          ClassLoader extensionsClassLoader = new ExtensionsClassLoader(
              sweetHome3DAppletClass.getClassLoader(), sweetHome3DAppletClass.getProtectionDomain(),
              (String [])java3DFiles.toArray(new String [java3DFiles.size()]), 
              (String [])applicationPackages.toArray(new String [applicationPackages.size()]));
     
          // Call application constructor with reflection
          Class applicationClass = extensionsClassLoader.loadClass(applicationClassName);
          Constructor applicationConstructor = 
              applicationClass.getConstructor(new Class [] {JApplet.class});
          this.appletApplication = applicationConstructor.newInstance(new Object [] {this});
        } catch (Throwable ex) {
          if (ex instanceof InvocationTargetException) {
            ex = ((InvocationTargetException)ex).getCause();
          }
          if (ex instanceof AccessControlException) {
            showError("<html>If you want to run this applet, you must relaunch your browser,"               
                + "<br>display this page again and accept the displayed digital signature.");
          } else {
            showError("<html>Can't start applet:<br>Exception" 
                + ex.getClass().getName() + " " + ex.getMessage());
            ex.printStackTrace();
          }
        }
     
      }
     
     
      /**
       * Returns the name of the {@linkplain AppletApplication application} class associated to this applet. 
       * This class must have a constructor taking in parameter a <code>JApplet</code>. 
       */
      protected String getApplicationClassName() {
        return "com.eteks.sweethome3d.applet.AppletApplication";
      }  
     
      /**
       * Returns the collection of packages that are found in plugins. 
       */
     
     
      private Collection getPluginsPackages() {
     
        System.out.println("simo get pluginsPackages");
     
        String pluginURLs = getParameter("pluginURLs");
        if (pluginURLs != null) {        
          Set pluginPackages = new HashSet();
          // Add to pluginPackages all the packages contained in the plugin URLs
          String [] urlStrings = pluginURLs.split("\\s|,");
          for (int i = 0; i < urlStrings.length; i++) {
            try {
              URL pluginUrl = new URL(getCodeBase(), urlStrings [i]);
              ZipInputStream zipIn = null;
              try {
                // Open a zip input from pluginUrl
                zipIn = new ZipInputStream(pluginUrl.openStream());
                // Try directories in current zip stream  
                for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
                  String zipEntryName = entry.getName();
                  int lastIndex = zipEntryName.lastIndexOf('/');
                  if (zipEntryName.endsWith(".class")) {
                    if (lastIndex == -1) {
                      pluginPackages.add(""); // Add empty package
                    } else {
                      pluginPackages.add(zipEntryName.substring(0, lastIndex).replace('/', '.'));
                    }
                  }
                }
              } catch (IOException ex) {
                // Ignore furniture plugin 
              } finally {
                if (zipIn != null) {
                  try {
                    zipIn.close();
                  } catch (IOException ex) {
                  }
                }
              }
            } catch (MalformedURLException ex) {
              // Ignore malformed URLs
            }
          }
          return pluginPackages;
        }
     
        return Collections.EMPTY_SET;
     
      }
     
    }
    Donc j'espère que quelqu'un aura une idée sur ce problème.
    Merci d'avance!

  2. #2
    Membre du Club
    Homme Profil pro
    Inscrit en
    Février 2010
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2010
    Messages : 44
    Points : 47
    Points
    47
    Par défaut
    Tu peux nous montrer ton fichier HTML ?

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 15
    Points : 7
    Points
    7
    Par défaut
    voilà le fichier html:

    <HTML>
    <HEAD>
    <TITLE>Applet de test</TITLE>
    </HEAD>
    <BODY>
    <HR>
    <APPLET CODE="SweetHome3DApplet.class" WIDTH="300" HEIGHT="100">
    </APPLET>
    <HR>
    </BODY>
    </HTML>

    j'ai pu avancé dans ce problème et j'ai réalisé qu'il est dû au contenue de la classe SweetHome3DApplet.class et plus précisément c'est à cause de l'appel du constructeur de la classe ExtensionsClassLoader.class dans la partie du code au dessus que j'ai reprise ici:

    ClassLoader extensionsClassLoader = new ExtensionsClassLoader(
    sweetHome3DAppletClass.getClassLoader(), sweetHome3DAppletClass.getProtectionDomain(),
    (String [])java3DFiles.toArray(new String [java3DFiles.size()]),
    (String [])applicationPackages.toArray(new String [applicationPackages.size()]));

    //Call application constructor with reflection

    Class applicationClass = extensionsClassLoader.loadClass(applicationClassName);
    Constructor applicationConstructor =applicationClass.getConstructor(new Class [] {JApplet.class});
    this.appletApplication = applicationConstructor.newInstance(new Object [] {this});


    et voila la définition de la classe ExtensionsClassLoader.class :

    package com.eteks.sweethome3d.tools;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.security.ProtectionDomain;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;

    /**
    * Class loader able to load classes and DLLs with a higher priority from a given set of JARs.
    * Its bytecode is Java 1.1 compatible to be loadable by old JVMs.
    * @author Emmanuel Puybaret
    */

    public class ExtensionsClassLoader extends ClassLoader {

    private final ProtectionDomain protectionDomain;
    private final String [] applicationPackages;


    private final Map extensionDlls = new HashMap();
    private JarFile [] extensionJars = null;

    /**
    * Creates a class loader. It will consider JARs and DLLs of <code>extensionJarsAndDlls</code>
    * as classpath and libclasspath elements with a higher priority than the ones of default classpath,
    * and will load itself all the classes belonging to packages of <code>applicationPackages</code>.
    */

    public ExtensionsClassLoader(ClassLoader parent,
    ProtectionDomain protectionDomain,
    String [] extensionJarsAndDlls,
    String [] applicationPackages) {

    super(parent);
    this.protectionDomain = protectionDomain;
    this.applicationPackages = applicationPackages;

    // Compute DLLs prefix and suffix
    String dllSuffix;
    String dllPrefix;

    String osName = System.getProperty("os.name");

    if (osName.startsWith("Windows")) {
    dllSuffix = ".dll";
    dllPrefix = "";
    } else if (osName.startsWith("Mac OS X")) {
    dllSuffix = ".jnilib";
    dllPrefix = "lib";
    } else {
    dllSuffix = ".so";
    dllPrefix = "lib";
    }

    // Find extension Jars and DLLs
    ArrayList extensionJars = new ArrayList();
    for (int i = 0; i < extensionJarsAndDlls.length; i++) {
    String extensionJarOrDll = extensionJarsAndDlls [i];
    try {
    URL extensionJarOrDllUrl = getResource(extensionJarOrDll);
    if (extensionJarOrDllUrl != null) {
    if (extensionJarOrDll.endsWith(".jar")) {
    // Copy jar to a tmp file
    String extensionJar = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), ".jar");
    // Add tmp file to extension jars list
    extensionJars.add(new JarFile(extensionJar, false));
    } else if (extensionJarOrDll.endsWith(dllSuffix)) {
    int lastSlashIndex = extensionJarOrDll.lastIndexOf('/');
    // Copy DLL to a tmp file
    String extensionDll = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), dllSuffix);
    // Add tmp file to extension DLLs map
    this.extensionDlls.put(extensionJarOrDll.substring(lastSlashIndex + 1 + dllPrefix.length(),
    extensionJarOrDll.indexOf(dllSuffix)), extensionDll);
    }
    }
    } catch (IOException ex) {
    throw new RuntimeException("Couldn't extract extension jars", ex);
    }
    }
    // Create extensionJars array
    if (extensionJars.size() > 0) {
    this.extensionJars = (JarFile [])extensionJars.toArray(new JarFile [extensionJars.size()]);
    }

    }

    /**
    * Returns the file name of a temporary copy of <code>input</code> content.
    */
    private String copyInputStreamToTmpFile(InputStream input,
    String suffix) throws IOException {
    File tmpFile = File.createTempFile("extension", suffix);
    tmpFile.deleteOnExit();
    OutputStream output = null;
    try {
    output = new BufferedOutputStream(new FileOutputStream(tmpFile));
    byte [] buffer = new byte [8192];
    int size;
    while ((size = input.read(buffer)) != -1) {
    output.write(buffer, 0, size);
    }
    } finally {
    if (input != null) {
    input.close();
    }
    if (output != null) {
    output.close();
    }
    }
    return tmpFile.toString();
    }

    /**
    * Finds and defines the given class among the extension JARs
    * given in constructor, then among resources.
    */

    protected Class findClass(String name) throws ClassNotFoundException { //protected
    // Build class file from its name
    String classFile = name.replace('.', '/') + ".class";
    InputStream classInputStream = null;
    if (this.extensionJars != null) {
    // Check if searched class is an extension class
    for (int i = 0; i < this.extensionJars.length; i++) {
    JarFile extensionJar = this.extensionJars [i];
    JarEntry jarEntry = extensionJar.getJarEntry(classFile);
    if (jarEntry != null) {
    try {
    classInputStream = extensionJar.getInputStream(jarEntry);
    } catch (IOException ex) {
    throw new ClassNotFoundException("Couldn't read class " + name, ex);
    }
    }
    }
    }
    // If it's not an extension class, search if its an application
    // class that can be read from resources
    if (classInputStream == null) {
    URL url = getResource(classFile);
    if (url == null) {
    throw new ClassNotFoundException("Class " + name);
    }
    try {
    classInputStream = url.openStream();
    } catch (IOException ex) {
    throw new ClassNotFoundException("Couldn't read class " + name, ex);
    }
    }

    try {
    // Read class input content to a byte array
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedInputStream in = new BufferedInputStream(classInputStream);
    byte [] buffer = new byte [8192];
    int size;
    while ((size = in.read(buffer)) != -1) {
    out.write(buffer, 0, size);
    }
    in.close();
    // Define class
    return defineClass(name, out.toByteArray(), 0, out.size(),
    this.protectionDomain);
    } catch (IOException ex) {
    throw new ClassNotFoundException("Class " + name, ex);
    }
    }

    /**
    * Returns the library path of an extension DLL.
    */
    protected String findLibrary(String libname) {//protected
    return (String)this.extensionDlls.get(libname);
    }

    /**
    * Returns the URL of the given resource searching first if it exists among
    * the extension JARs given in constructor.
    */
    protected URL findResource(String name) {//protected
    if (this.extensionJars != null) {
    // Try to find if resource belongs to one of the extracted jars
    for (int i = 0; i < this.extensionJars.length; i++) {
    JarFile extensionJar = this.extensionJars [i];
    JarEntry jarEntry = extensionJar.getJarEntry(name);
    if (jarEntry != null) {
    try {
    return new URL("jar:file:" + extensionJar.getName() + ":" + jarEntry.getName());
    } catch (MalformedURLException ex) {
    // Forget that we could have found a resource
    }
    }
    }
    }
    return super.findResource(name);
    }

    /**
    * Loads a class with this class loader if its package belongs to <code>applicationPackages</code>
    * given in constructor.
    */

    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {//protected
    // If no extension jars couldn't be found
    if (this.extensionJars == null) {
    // Let default class loader do its job
    return super.loadClass(name, resolve);
    }
    // Check if the class has already been loaded
    Class loadedClass = findLoadedClass(name);
    if (loadedClass == null) {
    try {
    // Try to find if class belongs to one of the application packages
    for (int i = 0; i < this.applicationPackages.length; i++) {
    String applicationPackage = this.applicationPackages [i];
    int applicationPackageLength = applicationPackage.length();
    if ( (applicationPackageLength == 0
    && name.indexOf('.') == 0)
    || (applicationPackageLength > 0
    && name.startsWith(applicationPackage))) {
    loadedClass = findClass(name);
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    // Let a chance to class to be loaded by default implementation
    }
    if (loadedClass == null) {
    loadedClass = super.loadClass(name, resolve);
    }
    }
    if (resolve) {
    resolveClass(loadedClass);
    }
    return loadedClass;
    }
    }

    Merci d'avance!

  4. #4
    Membre du Club
    Homme Profil pro
    Inscrit en
    Février 2010
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2010
    Messages : 44
    Points : 47
    Points
    47
    Par défaut
    Pour moi, le problème est assez clair. Il est même marqué dans la trace d'erreur :

    java.lang.NoClassDefFoundError: SweetHome3DApplet (wrong name: com/eteks/sweethome3d/applet/SweetHome3DApplet)
    Ce problème vient du fait que SweetHome3DApplet.class se trouve dans un paquet et que, par conséquent, si tu le mets "à côtés" de ton fichier index.html il n'est pas à la bonne place. Pour résoudre ton problème, il faut :

    1. Modifie ton arborescence de fichiers pour qu'elle ressemble à ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    /
       |-- index.html
       |-- com/
          |-- eteks/
             |--sweethome3d/
                |--applet/
                   |-- SweetHome3DApplet.class

    2. Remplace le contenu de ton fichier HTML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <APPLET CODE="SweetHome3DApplet.class" WIDTH="300" HEIGHT="100">
    par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <applet codebase="." code="com.eteks.sweethome3d.applet.SweetHome3DApplet.class" width="300" height="100">

    Et comme je parie que sa va pas marcher de suite, j'enchaîne avec les questions à 1000 Frs :
    • Est-ce que SweetHome3DApplet.class provient d'une archive jar ?
    • Est-ce que SweetHome3DApplet.class est livré avec d'autre fichier .class ?

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 15
    Points : 7
    Points
    7
    Par défaut
    Bonjour butane et merci pour votre collaboration!
    1) oui, SweetHome3DApplet.class vient d'une archive jar.
    2) oui, SweetHome3DApplet.class est livré avec d'autre fichiers .class

    j'ai suivi le conseil que tu m'avais donné mais sa marche pas; le navigateur m'affiche le msg suivant:


    charger : classe com.eteks.sweethome3d.applet.SweetHome3D.class introuvable.
    java.lang.ClassNotFoundException: com.eteks.sweethome3d.applet.SweetHome3D.class
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.io.FileNotFoundException: D:\eclipse\Workspace\SweetHome3D\classes\com\eteks\sweethome3d\applet\SweetHome3D\class.class (Le chemin d’accès spécifié est introuvable)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.http://www.protocol.file.FileURLConn...onnect(Unknown Source)
    at sun.net.http://www.protocol.file.FileURLConn...Stream(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 7 more
    Exception : java.lang.ClassNotFoundException: com.eteks.sweethome3d.applet.SweetHome3D.class
    Mais au fait j'ai pu avancé dans ce problème; j'ai fait une copie de cette classe dans le package par défaut et la le navigateur arrive à localiser la classe mais il réclame qu'il y a un problème dans le code source de la classe.
    par la suite j'ai réalisé que c'est dû à l'appel du constructeur de la classe ExtensionsClassLoader.class que vous voyez dans la définition de SweetHome3DApplet.class (ci-dessus)
    Et plus précisément l'erreur est au niveau du 2ème paramètre du constructeur ExtensionsClassLoader(ClassLoader parent,
    ProtectionDomain protectionDomain,
    String [] extensionJarsAndDlls,
    String [] applicationPackages)
    càd quand on lui passe la valeur sweetHome3DAppletClass.getProtectionDomain() .
    voila l'erreur qu'il m'affiche eclipse (il exécute quand même l'applet):

    ProtectionDomain (file:/D:/eclipse/Workspace/SweetHome3D/classes/ <no signer certificates>)
    sun.misc.Launcher$AppClassLoader@11b86e7
    <no principals>
    java.security.Permissions@1abc7b9 (
    (java.security.AllPermission <all permissions> <all actions>)
    (java.net.SocketPermission localhost:1024- listen,resolve)
    (java.lang.RuntimePermission stopThread)
    (java.lang.RuntimePermission exitVM)
    (java.util.PropertyPermission line.separator read)
    (java.util.PropertyPermission java.vm.version read)
    (java.util.PropertyPermission java.vm.specification.version read)
    (java.util.PropertyPermission java.vm.specification.vendor read)
    (java.util.PropertyPermission java.vendor.url read)
    (java.util.PropertyPermission java.vm.name read)
    (java.util.PropertyPermission os.name read)
    (java.util.PropertyPermission java.vm.vendor read)
    (java.util.PropertyPermission path.separator read)
    (java.util.PropertyPermission java.specification.name read)
    (java.util.PropertyPermission os.version read)
    (java.util.PropertyPermission os.arch read)
    (java.util.PropertyPermission java.class.version read)
    (java.util.PropertyPermission java.version read)
    (java.util.PropertyPermission file.separator read)
    (java.util.PropertyPermission java.vendor read)
    (java.util.PropertyPermission java.vm.specification.name read)
    (java.util.PropertyPermission java.specification.version read)
    (java.util.PropertyPermission java.specification.vendor read)
    (java.io.FilePermission \D:\eclipse\Workspace\SweetHome3D\classes\- read)
    )
    et voilà le msg qu'il m'affiche le navigateur:
    Access denied (java.lang.RuntimePermission getProtectionDomain) If you want to run this applet, you must relaunch your browser, display this page again and accept the displayed digital signature.
    j'ai essayé de décocher la case "Lecture seule" du dossier D:\eclipse\Workspace\SweetHome3D\classes mais la case revient automatiquement à son état initial "cochée" --> donc impossible de la décoché et je sé pas pourquoi.
    Et j'ai aussi redémarré le navigateur mais ça marche tjrs pas.
    voilà le code source de la classe ExtensionsClassLoader.class:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    package com.eteks.sweethome3d.tools;
     
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.security.ProtectionDomain;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
     
    /**
     * Class loader able to load classes and DLLs with a higher priority from a given set of JARs. 
     * Its bytecode is Java 1.1 compatible to be loadable by old JVMs.
     * @author Emmanuel Puybaret
     */
     
    public class ExtensionsClassLoader extends ClassLoader {
     
      private final ProtectionDomain protectionDomain;
      private final String []        applicationPackages;
     
     
      private final Map  extensionDlls = new HashMap();
      private JarFile [] extensionJars = null;
     
      /**
       * Creates a class loader. It will consider JARs and DLLs of <code>extensionJarsAndDlls</code>
       * as classpath and libclasspath elements with a higher priority than the ones of default classpath, 
       * and will load itself all the classes belonging to packages of <code>applicationPackages</code>.
       */
     
      public ExtensionsClassLoader(ClassLoader parent, 
                                   ProtectionDomain protectionDomain, 
                                   String [] extensionJarsAndDlls,
                                   String [] applicationPackages) {
     
        super(parent);
        this.protectionDomain = protectionDomain;
        this.applicationPackages = applicationPackages;
     
        // Compute DLLs prefix and suffix
        String dllSuffix;
        String dllPrefix;
     
        String osName = System.getProperty("os.name");
     
        if (osName.startsWith("Windows")) {
          dllSuffix = ".dll";
          dllPrefix = "";
        } else if (osName.startsWith("Mac OS X")) {
          dllSuffix = ".jnilib";
          dllPrefix = "lib";
        } else {
          dllSuffix = ".so";
          dllPrefix = "lib";
        }
     
        // Find extension Jars and DLLs
        ArrayList extensionJars = new ArrayList();
        for (int i = 0; i < extensionJarsAndDlls.length; i++) {
          String extensionJarOrDll = extensionJarsAndDlls [i];
          try {
            URL extensionJarOrDllUrl = getResource(extensionJarOrDll);
            if (extensionJarOrDllUrl != null) {
              if (extensionJarOrDll.endsWith(".jar")) {
                // Copy jar to a tmp file
                String extensionJar = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), ".jar");
                // Add tmp file to extension jars list
                extensionJars.add(new JarFile(extensionJar, false));
              } else if (extensionJarOrDll.endsWith(dllSuffix)) {
                int lastSlashIndex  = extensionJarOrDll.lastIndexOf('/');
                // Copy DLL to a tmp file
                String extensionDll = copyInputStreamToTmpFile(extensionJarOrDllUrl.openStream(), dllSuffix);
                // Add tmp file to extension DLLs map
                this.extensionDlls.put(extensionJarOrDll.substring(lastSlashIndex + 1 + dllPrefix.length(), 
                    extensionJarOrDll.indexOf(dllSuffix)), extensionDll);
              }          
            }
          } catch (IOException ex) {
            throw new RuntimeException("Couldn't extract extension jars", ex);
          }
        }
        // Create extensionJars array
        if (extensionJars.size() > 0) {
          this.extensionJars = (JarFile [])extensionJars.toArray(new JarFile [extensionJars.size()]);                    
        }
     
      }
     
      /**
       * Returns the file name of a temporary copy of <code>input</code> content.
       */
      private String copyInputStreamToTmpFile(InputStream input, 
                                              String suffix) throws IOException {
        File tmpFile = File.createTempFile("extension", suffix);
        tmpFile.deleteOnExit();
        OutputStream output = null;
        try {
          output = new BufferedOutputStream(new FileOutputStream(tmpFile));
          byte [] buffer = new byte [8192];
          int size; 
          while ((size = input.read(buffer)) != -1) {
            output.write(buffer, 0, size);
          }
        } finally {
          if (input != null) {
            input.close();
          }
          if (output != null) {
            output.close();
          }
        }
        return tmpFile.toString();
      }
     
      /**
       * Finds and defines the given class among the extension JARs  
       * given in constructor, then among resources. 
       */
     
      protected Class findClass(String name) throws ClassNotFoundException { //protected
        // Build class file from its name 
        String classFile = name.replace('.', '/') + ".class";
        InputStream classInputStream = null;
        if (this.extensionJars != null) {
          // Check if searched class is an extension class
          for (int i = 0; i < this.extensionJars.length; i++) {
            JarFile extensionJar = this.extensionJars [i];
            JarEntry jarEntry = extensionJar.getJarEntry(classFile);
            if (jarEntry != null) {
              try {
                classInputStream = extensionJar.getInputStream(jarEntry);
              } catch (IOException ex) {
                throw new ClassNotFoundException("Couldn't read class " + name, ex);
              }
            }
          }
        }
        // If it's not an extension class, search if its an application 
        // class that can be read from resources
        if (classInputStream == null) {
          URL url = getResource(classFile);
          if (url == null) {
            throw new ClassNotFoundException("Class " + name);
          }
          try {
            classInputStream = url.openStream();
          } catch (IOException ex) {
            throw new ClassNotFoundException("Couldn't read class " + name, ex);
          }
        } 
     
        try {
          // Read class input content to a byte array
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          BufferedInputStream in = new BufferedInputStream(classInputStream);
          byte [] buffer = new byte [8192];
          int size; 
          while ((size = in.read(buffer)) != -1) {
            out.write(buffer, 0, size);
          }
          in.close();
          // Define class
          return defineClass(name, out.toByteArray(), 0, out.size(), 
              this.protectionDomain);
        } catch (IOException ex) {
          throw new ClassNotFoundException("Class " + name, ex);
        }
      }
     
      /**
       * Returns the library path of an extension DLL.
       */
      protected String findLibrary(String libname) {//protected
        return (String)this.extensionDlls.get(libname);
      }
     
      /**
       * Returns the URL of the given resource searching first if it exists among 
       * the extension JARs given in constructor. 
       */
      protected URL findResource(String name) {//protected
        if (this.extensionJars != null) {
          // Try to find if resource belongs to one of the extracted jars
          for (int i = 0; i < this.extensionJars.length; i++) {
            JarFile extensionJar = this.extensionJars [i];
            JarEntry jarEntry = extensionJar.getJarEntry(name);
            if (jarEntry != null) {
              try {
                return new URL("jar:file:" + extensionJar.getName() + ":" + jarEntry.getName());
              } catch (MalformedURLException ex) {
                // Forget that we could have found a resource
              }
            }
          }
        }
        return super.findResource(name);
      }
     
      /**
       * Loads a class with this class loader if its package belongs to <code>applicationPackages</code>
       * given in constructor.
       */
     
      protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {//protected
        // If no extension jars couldn't be found
        if (this.extensionJars == null) {
          // Let default class loader do its job
          return super.loadClass(name, resolve);
        }
        // Check if the class has already been loaded
        Class loadedClass = findLoadedClass(name);
        if (loadedClass == null) {
          try {
            // Try to find if class belongs to one of the application packages
            for (int i = 0; i < this.applicationPackages.length; i++) {
              String applicationPackage = this.applicationPackages [i];
              int applicationPackageLength = applicationPackage.length();
              if (   (applicationPackageLength == 0 
                     && name.indexOf('.') == 0)
                  || (applicationPackageLength > 0
                     && name.startsWith(applicationPackage))) {
                loadedClass = findClass(name);
                break;
              }
            }
          } catch (ClassNotFoundException ex) {
            // Let a chance to class to be loaded by default implementation
          }
          if (loadedClass == null) {
            loadedClass = super.loadClass(name, resolve);
          }
        }
        if (resolve) {
          resolveClass(loadedClass);
        }
        return loadedClass;
      }
    }
    Merci d'avance!

  6. #6
    Membre du Club
    Homme Profil pro
    Inscrit en
    Février 2010
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2010
    Messages : 44
    Points : 47
    Points
    47
    Par défaut
    1) oui, SweetHome3DApplet.class vient d'une archive jar.
    2) oui, SweetHome3DApplet.class est livré avec d'autre fichiers .class


    Je pense que le problème est que SweetHome3DApplet.class a besoins d'autre class se trouvant dans le jar... Le plus simple, c'est d'appeler le SweetHome3DApplet.class se trouvant dans le jar :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <applet codebase="." code="com.eteks.sweethome3d.applet.SweetHome3DApplet.class" archive="lenomdetonarchive.jar" width="300" height="100">

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 15
    Points : 7
    Points
    7
    Par défaut
    j'ai mis:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <APPLET  codebase="." code="com.eteks.sweethome3d.applet.SweetHome3DApplet.class" archive="D:\eclipse\Workspace\SweetHome3D.jar" WIDTH="300" HEIGHT="100">
    où D:\eclipse\Workspace\SweetHome3D représente le dossier où ya tt les fichiers du projet.
    D:
    |eclipse
    - |workspace
    ------|.metadata
    ----------|SweetHome3D
    --------------|.settings
    --------------|classes
    --------------|deploy
    --------------|install
    --------------|lib
    --------------|libtest
    --------------|src
    ----------------|com
    --------------|test
    -----------------|com

    mais ça marche pas. le navigateur m'affiche le msg:

    java.net.MalformedURLException: unknown protocol: d
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at sun.plugin.util.GrayBoxPainter.setProgressFilter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.setupGrayBoxPainter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.access$600(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    exception : name.
    java.lang.IllegalArgumentException: name
    at sun.plugin2.applet.Applet2ClassLoader.addJar(Unknown Source)
    at sun.plugin2.applet.Applet2Manager.loadJarFiles(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception : java.lang.IllegalArgumentException: name

Discussions similaires

  1. Réponses: 5
    Dernier message: 21/12/2009, 21h31
  2. Réponses: 0
    Dernier message: 10/02/2009, 09h49
  3. Réponses: 1
    Dernier message: 27/11/2008, 16h47
  4. Appeler une Applet ou servlet depuis une classe java
    Par oussam dans le forum Applets
    Réponses: 2
    Dernier message: 04/07/2006, 13h58
  5. Générer une classe java à partir d'une autre
    Par hebmaster dans le forum Général Java
    Réponses: 5
    Dernier message: 29/05/2006, 16h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo