Hadoop Common
  1. Hadoop Common
  2. HADOOP-10063

RunJar ClassLoader cached the class can't release

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.0.3, 2.2.0
    • Fix Version/s: None
    • Component/s: util
    • Labels:
      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);
      		}
      

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Unassigned
            Reporter:
            Joe Au
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:

              Development