Uploaded image for project: 'Hadoop Common'
  1. Hadoop Common
  2. HADOOP-10063

RunJar ClassLoader cached the class can't release

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.0.3, 2.2.0
    • None
    • util
    • None

    Description

      At the end of method main()

      RunJar.java
          ClassLoader loader =
            new URLClassLoader(classPath.toArray(new URL[0]));
      
          Thread.currentThread().setContextClassLoader(loader);
          Class<?> mainClass = Class.forName(mainClassName, true, loader);
          Method main = mainClass.getMethod("main", new Class[] {
            Array.newInstance(String.class, 0).getClass()
          });
          String[] newArgs = Arrays.asList(args)
            .subList(firstArg, args.length).toArray(new String[0]);
          try {
            main.invoke(null, new Object[] { newArgs });
          } catch (InvocationTargetException e) {
            throw e.getTargetException();
          }
      

      The ClassLoader cached the class can't release.Because the development time class code will often changes, so will be very inconvenient.

      Rewrite as follows:

      RunJar.java
      		ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
      		ClassLoader loader = new URLClassLoader(classPath.toArray(new URL[0]), prevCl);
      
      		try {
      			Thread.currentThread().setContextClassLoader(loader);
      
      			Class<?> mainClass = Class.forName(mainClassName, true, loader);
      			Method main = mainClass.getMethod("main", new Class[] { Array
      					.newInstance(String.class, 0).getClass() });
      			String[] newArgs = Arrays.asList(args).subList(firstArg, args.length)
      					.toArray(new String[0]);
      			
      			main.invoke(null, new Object[] { newArgs });
      		} catch (InvocationTargetException e) {
      			throw e.getTargetException();
      		} finally {
      			// Restore
      			Thread.currentThread().setContextClassLoader(prevCl);
      		}
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            JoeAu Joe Au
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: