<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: jar code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 22:43:34 GMT</pubDate>
    <description>DZone Snippets: jar code</description>
    <item>
      <title>Which class file is loaded by the classloader ?</title>
      <link>http://snippets.dzone.com/posts/show/3719</link>
      <description>When using lots of third-party libraries, one problem might be that 2 of them package different versions of the same class, producing errors when method version conflicts happen.&lt;br /&gt;&lt;br /&gt;Here is a simple way to know the exact location used by the classloader to get your class :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;URL myClassURL = MyMysteryClass.class.getProtectionDomain().getCodeSource().getLocation();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Mar 2007 11:38:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3719</guid>
      <author>nivek (Kevin Gaudin)</author>
    </item>
    <item>
      <title>Add a jar file to Java load path at run time</title>
      <link>http://snippets.dzone.com/posts/show/3574</link>
      <description>Sometimes it is necessary to amend the class load path at run time. For example, dynamically adding jar files containing user-configurable JDBC data sources. The way this is done is truly monstrous -- the URL Path format was only revealed when a colleague looked into the Java library sources.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.net.URL;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.net.URLClassLoader;&lt;br /&gt;import java.net.MalformedURLException;&lt;br /&gt;&lt;br /&gt;public class JarFileLoader extends URLClassLoader&lt;br /&gt;{&lt;br /&gt;    public JarFileLoader (URL[] urls)&lt;br /&gt;    {&lt;br /&gt;        super (urls);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void addFile (String path) throws MalformedURLException&lt;br /&gt;    {&lt;br /&gt;        String urlPath = "jar:file://" + path + "!/";&lt;br /&gt;        addURL (new URL (urlPath));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static void main (String args [])&lt;br /&gt;    {&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            System.out.println ("First attempt...");&lt;br /&gt;            Class.forName ("org.gjt.mm.mysql.Driver");&lt;br /&gt;        }&lt;br /&gt;        catch (Exception ex)&lt;br /&gt;        {&lt;br /&gt;            System.out.println ("Failed.");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            URL urls [] = {};&lt;br /&gt;&lt;br /&gt;            JarFileLoader cl = new JarFileLoader (urls);&lt;br /&gt;            cl.addFile ("/opt/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar");&lt;br /&gt;            System.out.println ("Second attempt...");&lt;br /&gt;            cl.loadClass ("org.gjt.mm.mysql.Driver");&lt;br /&gt;            System.out.println ("Success!");&lt;br /&gt;        }&lt;br /&gt;        catch (Exception ex)&lt;br /&gt;        {&lt;br /&gt;            System.out.println ("Failed.");&lt;br /&gt;            ex.printStackTrace ();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Feb 2007 15:24:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3574</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Locate the jar in the classpath for a given class</title>
      <link>http://snippets.dzone.com/posts/show/3472</link>
      <description>This class has a single static method which when passed a class will return the location of the Jar file that loaded that class.  Basically a refactoring of Laird Nelson's code from http://weblogs.java.net/blog/ljnelson/archive/2004/09/cheap_hack_i_re.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.net.URL;&lt;br /&gt;import java.util.regex.Matcher;&lt;br /&gt;import java.util.regex.Pattern;&lt;br /&gt;&lt;br /&gt;public class FindJarForClass&lt;br /&gt;{&lt;br /&gt;  private FindJarForClass() {}&lt;br /&gt;&lt;br /&gt;  public static String locate( Class c ) throws ClassNotFoundException&lt;br /&gt;  {&lt;br /&gt;    final URL location;&lt;br /&gt;    final String classLocation = c.getName().replace('.', '/') + ".class";&lt;br /&gt;    final ClassLoader loader = c.getClassLoader();&lt;br /&gt;    if( loader == null )&lt;br /&gt;    {&lt;br /&gt;      location = ClassLoader.getSystemResource(classLocation);&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;      location = loader.getResource(classLocation);&lt;br /&gt;    }&lt;br /&gt;    if( location != null ) &lt;br /&gt;    {&lt;br /&gt;      Pattern p = Pattern.compile( "^.*:(.*)!.*$" ) ;&lt;br /&gt;      Matcher m = p.matcher( location.toString() ) ;&lt;br /&gt;      if( m.find() )&lt;br /&gt;        return m.group( 1 ) ;&lt;br /&gt;      else&lt;br /&gt;        throw new ClassNotFoundException( "Cannot parse location of '" + location + "'.  Probably not loaded from a Jar" ) ;&lt;br /&gt;    }&lt;br /&gt;    else &lt;br /&gt;      throw new ClassNotFoundException( "Cannot find class '" + c.getName() + " using the classloader" ) ;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 07 Feb 2007 20:11:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3472</guid>
      <author>bloid (Tim Yates)</author>
    </item>
    <item>
      <title>Java - imgFromJar</title>
      <link>http://snippets.dzone.com/posts/show/2849</link>
      <description>// Inserire un immagine presente nel file jar dell'applicazione&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;try&lt;br /&gt;		{&lt;br /&gt;			icon = ImageIO.read(getClass().getResourceAsStream("imgs/icon.png")); // La preleva dal file jar&lt;br /&gt;			setIconImage(icon);&lt;br /&gt;		}&lt;br /&gt;		catch(IOException e)&lt;br /&gt;		{&lt;br /&gt;			e.printStackTrace();&lt;br /&gt;		}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 17 Oct 2006 14:23:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2849</guid>
      <author>whitetiger ()</author>
    </item>
  </channel>
</rss>
