Loading An Elements From JAR

I'm developing an application using Eclipse IDE. I've the following method:


 public LinkedList<String> getClusteringAlgorithms() throws Exception {
  StaticUtils.log("Getting clustering algorithms...");
  LinkedList<String> ret = new LinkedList<String>();
  File classesDir = new File(Constants.CLUSTERING_ALGORITHMS_DIR);
  File classes[] = classesDir.listFiles();
  if (classes != null) {
   for (int i = 0; i < classes.length; i++) {
    if (classes[i].isFile())
     if (!classes[i].getName().equalsIgnoreCase(
       && (!classes[i].getName().equalsIgnoreCase(
         Constants.CLUSTERING_ALGORITHM_PARMS))) {

  return ret;


Class Constants:

 public static final String CLUSTERING_ALGORITHMS_DIR = "bin/es/uc3m/beruby/signal/clustering/algorithms/";
 public static final String CLUSTERING_ALGORITHM_INTERFACE_CLASS = "ClusteringInterface.class";
 public static final String CLUSTERING_ALGORITHM 

This method, basically, allows me to get the "avalaible" clustering algorithms looking into the folder where the ".class" of the classes associated to the algorithms will be.

When I execute my application from Eclipse everything works fine. However, I want to make the application avalaible out of eclipse (via JAR file) and here is when the problems begins.

If I create the JAR from eclipse, I told eclipse to create a separate folder to include the libraries associated to the project and I create the JAR.

When I execute the application it can't load the clustering algorithms (I suppose because the path of CLUSTERING_ALGORITHMS_DIR is different when you are executing it from a JAR file).

How to solve this problem?


For finding classes within the classpath (which could be the classes folder, or inside a jar file), I have done similar using the "scannotation" project. The idea is to read look at the class files without actually loading them, because that wastes the class loader area of the java vm memory. Where scannotation project aims to find classes tagged with a given annotation, perhaps the ClasspathUrlFinder would be a good example of how to find the starting point of classes in the classpath without having to hard code the file system path. Perhaps the logic could be extended to find classes that implement the clutering interface.

Another idea, if you assume the algorithms folder is always in the same folder where your application.jar file is, and when you run the application.jar file, we can determine the file system location for where ever this is with something like :


   * Constructs a path expression for the location on the file system to the jar file that contains this current class.
   * Stuff this into a class that ends up in your jar file.
   * @return e.g. /home/users/myapp/  if this class was contained in /home/users/myapp/foo.jar
  public static final String getJarPath() {
    Class<?> cls = AbstractLauncher.class;
    ProtectionDomain domain = cls.getProtectionDomain();
    CodeSource codeSource = domain.getCodeSource();
    URL sourceLocation = codeSource.getLocation();
    String path = sourceLocation.getPath();
    path = path.substring(0, path.lastIndexOf("/"));
    if (path.endsWith(".jar!")) {
      path = path.substring(0, path.lastIndexOf("/"));
    if (path.startsWith("file:")) {
      path = path.substring("file:".length());
    if (!path.endsWith("/")) {
      path += "/";

    return path;

   * Example use of the getJarPath
  public static void main(String[] args) {
    System.out.println("My folder containing my jar file: " + getJarPath());

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.