3333import java .util .concurrent .ConcurrentHashMap ;
3434import java .util .jar .JarEntry ;
3535import java .util .jar .JarFile ;
36+ import java .util .jar .Manifest ;
3637import java .io .File ;
3738import java .io .IOException ;
3839
5657 * must for obvious reasons be stored directly inside the bundle jar;
5758 * i.e not within an embedded jar.)
5859 *
60+ * <p/>
61+ * All jars given as part of the classpath will be recursively
62+ * inspected for embedded jars, and these will in turn be added to the
63+ * classpath. Jar files on the classpath having manifests declaring
64+ * <strong><code>Premain-Class:</code></strong> (i.e Java Agent jars)
65+ * will not be inspected by default, but can be added to the
66+ * EmJar-specific classpath contained in the
67+ * <strong><code>emjar.class.path</code></strong> property if this is
68+ * desired.
69+
5970 * <p/>
6071 * For a less manual approach that embeds all configuration in the
6172 * bundled jar, see {@link Boot}.
@@ -115,16 +126,19 @@ private static URL[] getClassPath(final Properties props, final Handler handler)
115126 DEBUG = "true" .equalsIgnoreCase (props .getProperty (EMJAR_LOG_DEBUG_PROP , "" ));
116127
117128 final ArrayList <URL > urls = new ArrayList <>();
118- addClassPathUrls (props .getProperty (JAVA_CLASS_PATH_PROP ), urls , handler );
119- addClassPathUrls (props .getProperty (EMJAR_CLASS_PATH_PROP ), urls , handler );
129+ addClassPathUrls (props .getProperty (JAVA_CLASS_PATH_PROP ), urls , handler , false );
130+ addClassPathUrls (props .getProperty (EMJAR_CLASS_PATH_PROP ), urls , handler , true );
120131 if (DEBUG ) {
121132 System .err .println ("EmJar: using classpath " + urls );
122133 }
123134 return urls .toArray (new URL [0 ]);
124135 }
125136
126137 private static void addClassPathUrls (
127- final String classPath , final List <URL > urls , final Handler handler ) {
138+ final String classPath ,
139+ final List <URL > urls ,
140+ final Handler handler ,
141+ final boolean force ) {
128142 if (classPath == null ) {
129143 return ;
130144 }
@@ -136,6 +150,15 @@ private static void addClassPathUrls(
136150 continue ;
137151 }
138152 final JarFile jar = new JarFile (file );
153+ final Manifest mf = jar .getManifest ();
154+ if (mf != null ) {
155+ if ((mf .getMainAttributes ().getValue ("Premain-Class" ) != null ) && !force ) {
156+ if (DEBUG ) {
157+ System .err .println ("EmJar: skipping java agent jar: " + elem );
158+ }
159+ continue ;
160+ }
161+ }
139162 final Enumeration <JarEntry > embedded = jar .entries ();
140163 while (embedded .hasMoreElements ()) {
141164 final JarEntry entry = embedded .nextElement ();
0 commit comments