You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-dev@hadoop.apache.org by "Joe Au (JIRA)" <ji...@apache.org> on 2013/10/22 11:53:43 UTC

[jira] [Created] (HADOOP-10063) RunJar ClassLoader cached the class can't release

Joe Au created HADOOP-10063:
-------------------------------

             Summary: RunJar ClassLoader cached the class can't release
                 Key: HADOOP-10063
                 URL: https://issues.apache.org/jira/browse/HADOOP-10063
             Project: Hadoop Common
          Issue Type: Improvement
          Components: util
    Affects Versions: 2.2.0, 1.0.3
            Reporter: Joe Au


At the end of method main()

{code:title=RunJar.java|borderStyle=solid}
    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();
    }
{code}
The ClassLoader cached the class can't release.Because the development time code will often changes, so will be very inconvenient.

Rewrite as follows:

{code:title=RunJar.java|borderStyle=solid}
		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);
		}
{code}



--
This message was sent by Atlassian JIRA
(v6.1#6144)