5 Replies Latest reply: Jul 27, 2010 5:13 AM by 843798 RSS

    Load jar in ClassLoad

    843798
      I need load jar in ClassLoad, I did write code, but he not work. what is the problem?
      package jar;
      
      import java.io.File;
      import java.io.IOException;
      import java.lang.reflect.Method;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLClassLoader;
      import java.util.Enumeration;
      import java.util.jar.JarFile;
      import java.util.zip.ZipEntry;
      
      public class Jar1 {
          public static void main(String[] args) {
              try {
                  String filePath = "/home/pitbull/temp/plugins/script.jar";
                  printJar(filePath);
                  load(filePath);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          public static void printJar(String path) throws IOException {
              JarFile jarFile = new JarFile(path);
              Enumeration entries = jarFile.entries();
              while (entries.hasMoreElements()) {
                  ZipEntry entry = (ZipEntry) entries.nextElement();
                  System.out.println(entry.getName());
              }
              jarFile.close();
          }
      
          public static void load(String filePath) throws MalformedURLException, ClassNotFoundException {
              filePath = "jar:file://" + filePath + "!/";
              URL url = new File(filePath).toURI().toURL();
              URLClassLoader clazzLoader = new URLClassLoader(new URL[]{url});
              Class clazz = clazzLoader.loadClass("scripts.task_custom_field_value.MessageFormat");
              for (Method method : clazz.getMethods()) {
                  System.out.println(method.getName());
              }
          }
      }
      This jar "/home/pitbull/temp/plugins/script.jar" not classpath.
        • 1. Re: Load jar in ClassLoad
          EJP
          Looks OK. Doesn't work how?
          • 2. Re: Load jar in ClassLoad
            843798
            Get exception. But class scripts.task_custom_field_value.MessageFormat.class is exactly have in jar.
            java.lang.ClassNotFoundException: scripts.task_custom_field_value.MessageFormat
                 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
                 at java.security.AccessController.doPrivileged(Native Method)
                 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
                 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
                 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
                 at jar.Jar1.load(Jar1.java:38)
                 at jar.Jar1.main(Jar1.java:18)
                 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                 at java.lang.reflect.Method.invoke(Method.java:597)
                 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
            Edited by: P1tBull on Jul 27, 2010 2:17 AM
            • 3. Re: Load jar in ClassLoad
              EJP
              Is it in the JAR under its package?
              • 4. Re: Load jar in ClassLoad
                843798
                Yes, it is. jar content:
                scripts/
                scripts/task_custom_field_value/
                scripts/task_custom_field_value/MessageFormat.class
                scripts/task_custom_field_value/FieldTesting.class
                scripts/before_add_message/
                scripts/before_add_message/CheckSpentTimeScript.class
                • 5. Re: Load jar in ClassLoad
                  843798
                  For create script.jar. I make following things:
                  mkdir src
                  cd src
                  mkdir script
                  cd script
                  mkdir task_custom_field_value
                  cd task_custom_field_value
                  touch MessageFormat.java
                  gedit MessageFormat.java
                  package script.task_custom_field_value;
                  
                  public class MessageFormat {
                      public MessageFormat() {
                      }
                  
                      public void echo() {
                          System.out.println("echo");
                      }
                  }
                  javac MessageFormat.java
                  cd ..
                  cd ..
                  cd ..
                  jar cf script.jar script/