<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: classpath code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 12:45:30 GMT</pubDate>
    <description>DZone Snippets: classpath code</description>
    <item>
      <title>Printing out the full path of a resource on the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4489</link>
      <description>// displaying the full path of a resource found by the class loader on the classpath, here the log4j.xml file&lt;br /&gt;&lt;code&gt;&lt;br /&gt;String resourceName = "/log4j.xml"; // pay attention to the leading '/' !&lt;br /&gt;URL location = AnyClass.class.getResource(resourceName);&lt;br /&gt;&lt;br /&gt;if (location != null) {&lt;br /&gt;    System.out.println(location.getPath());&lt;br /&gt;} else {&lt;br /&gt;    System.out.println(resourceName + " not found on the classpath");&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Sep 2007 09:59:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4489</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Loading a text file into a String from the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4480</link>
      <description>Locate a text file in the classpath and read it into a String&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    /** &lt;br /&gt;     * @param filePath      name of file to open. The file can reside&lt;br /&gt;     *                      anywhere in the classpath&lt;br /&gt;     */&lt;br /&gt;    private String readFileAsString(String filePath) throws java.io.IOException {&lt;br /&gt;        StringBuffer fileData = new StringBuffer(1000);&lt;br /&gt;        BufferedReader reader = new BufferedReader(new InputStreamReader(this&lt;br /&gt;            .getClass().getClassLoader().getResourceAsStream(filePath)));&lt;br /&gt;        char[] buf = new char[1024];&lt;br /&gt;        int numRead = 0;&lt;br /&gt;        while ((numRead = reader.read(buf)) != -1) {&lt;br /&gt;            String readData = String.valueOf(buf, 0, numRead);&lt;br /&gt;            fileData.append(readData);&lt;br /&gt;            buf = new char[1024];&lt;br /&gt;        }&lt;br /&gt;        reader.close();&lt;br /&gt;        return fileData.toString();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Aug 2007 12:48:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4480</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Loading a property file from the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4479</link>
      <description>Loads a property file located anywhere in the classpath&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    private Properties getPropertiesFromClasspath(String propFileName) throws IOException {&lt;br /&gt;        // loading xmlProfileGen.properties from the classpath&lt;br /&gt;        Properties props = new Properties();&lt;br /&gt;        InputStream inputStream = this.getClass().getClassLoader()&lt;br /&gt;            .getResourceAsStream(propFileName);&lt;br /&gt;&lt;br /&gt;        if (inputStream == null) {&lt;br /&gt;            throw new FileNotFoundException("property file '" + propFileName&lt;br /&gt;                + "' not found in the classpath");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        props.load(inputStream);&lt;br /&gt;&lt;br /&gt;        return props;&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Aug 2007 12:28:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4479</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</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>
  </channel>
</rss>
