Results 1 to 3 of 3
  1. #1
    alejandrorg is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Question Load elements from JAR

    Hello to all,

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

    Java Code:
    	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(
    							Constants.CLUSTERING_ALGORITHM_INTERFACE_CLASS)
    							&& (!classes[i].getName().equalsIgnoreCase(
    									Constants.CLUSTERING_ALGORITHM_PARMS))) {
    
    						ret.add(StaticUtils.removeExtension(classes[i]
    								.getName()));
    					}
    			}
    		}
    		return ret;
    	}
    Java Code:
    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_PARMS = "ClusteringAlgorithmParameter.class";
    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).

    Anyone can help me how to solve this problem?

    Thank you very much

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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 :

    Java Code:
     /**
       * 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());
      }

  3. #3
    alejandrorg is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Dear travishein,

    Thank you very much for your reply, I will try your solution!

    Best Regards,

    Alejandro Rodríguez

Similar Threads

  1. what does <Elements> mean
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-31-2009, 10:19 PM
  2. Missing Elements
    By princess in forum NetBeans
    Replies: 1
    Last Post: 10-29-2008, 11:51 PM
  3. using elements from other classes
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 07:25 AM
  4. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •