Showing posts from August, 2011

Accessing a file in a jar using

Obtaining links to files in jars is possible through 2 different method calls:
getClass().getResource(String name);
getClass().getResourceAsStream(String name);

The first method will return a URL to the file which won't be of any good to the class.
The second method will return an InputStream object to that file, which we will use. doesn't access files using InputStreams nor any File class that I know about. So we will have to create a file using the InputStream, then obtain a URL to it, then use to obtain a reference to that file.

Check the following method:

public void copyStreams(InputStream in, OutputStream out, int buffersize)
     throws IOException
     byte[] bytes = new byte[buffersize];
     int bytesRead =;
     while (bytesRead > -1) {
       out.write(bytes, 0, bytesRead);
       bytesRead =;
//I borrowed this method from a library I'm using, but it's working fine. The default bu…

How to create a JAR using Eclipse and use it in one of your projects

This post shows how to create a jar containing resources for other projects withing eclipse in particular. It will surely work with java projects developed in other IDEs but I specifically mention something extra regarding this situation.

It's assumed that you have at least some idea how Apache Ant. And of course have it installed on your machine.

1. Create a java project.
2. Have your resources (i.e. classes, files...etc) created and built.
3. Add a build.xml file in your project folder containing the following text:
<project name="ipKaizenCore" basedir="." default="clean-build">
    <property name="src.dir"     value="src">
    <property name="classes.dir" value="bin">
    <property name="jar.dir"     value="build">
    <target name="clean">
        <delete dir="${jar.dir}">
    <target name="compile"&g…