From ea2228c662a90cd28086f281a394fb3a3a0f860f Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 20 Nov 2008 19:12:26 +0000 Subject: [PATCH 1/1] Use context classloader Don't let the main class impacting the system properties git-svn-id: https://svn.argeo.org/slc/trunk@1836 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../org/argeo/slc/detached/AppLauncher.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/AppLauncher.java b/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/AppLauncher.java index 7eb4f48e7..d2e1ff51e 100644 --- a/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/AppLauncher.java +++ b/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/AppLauncher.java @@ -1,24 +1,29 @@ package org.argeo.slc.detached; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.List; import java.util.Properties; -import java.util.Vector; public class AppLauncher { private Properties systemProperties = new Properties(); private String mainClass = null; - private List arguments = new Vector(); + private List arguments = new ArrayList(); public void launch() { + + Properties base = System.getProperties(); + Properties fake = new Properties(base); + try { if (mainClass == null) throw new DetachedException( - "A main class name muste be specified."); + "A main class name has to be specified."); System.getProperties().putAll(systemProperties); - //Class clss = getClass().getClassLoader().loadClass(mainClass); - Class clss = Class.forName(mainClass); + + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + Class clss = cl.loadClass(mainClass); String[] args = new String[arguments.size()]; for (int i = 0; i < arguments.size(); i++) { @@ -28,11 +33,16 @@ public class AppLauncher { Class[] mainArgsClasses = new Class[] { args.getClass() }; Object[] mainArgs = { args }; Method mainMethod = clss.getMethod("main", mainArgsClasses); + + System.setProperties(fake); + mainMethod.invoke(null, mainArgs); } catch (Exception e) { throw new DetachedException("Unexpected exception while launching " + mainClass, e); + } finally { + System.setProperties(base); } } -- 2.39.2