Java Class Path getAbsolutePath(Class theClass, String path)

Here you can find the source of getAbsolutePath(Class theClass, String path)

Description

Finds the absolute path to a file from the classpath

License

Open Source License

Parameter

Parameter Description
object The object which classloader is used to retrieve the resource
path The relative path of the file

Exception

Parameter Description
NullPointerException If the file is not in the classpath

Return

A String with the absolute path.

Declaration

public static String getAbsolutePath(Class theClass, String path) throws NullPointerException 

Method Source Code

//package com.java2s;
/*//from   w  w  w.  j a  v a  2s.com
    
 This file is part of XleTView 
 Copyright (C) 2003 Martin Sveden
     
 This is free software, and you are 
 welcome to redistribute it under 
 certain conditions;
    
 See LICENSE document for details.
    
*/

import java.net.URL;
import java.net.URLDecoder;

public class Main {
    /**
     * Finds the absolute path to a file from the classpath 
     * @param object The object which classloader is used to retrieve the resource
     * @param path The relative path of the file
     * @return A String with the absolute path.
     * @throws NullPointerException If the file is not in the classpath
     */
    public static String getAbsolutePath(Class theClass, String path) throws NullPointerException {
        URL url = theClass.getClassLoader().getResource(path);
        if (url == null) {
            throw new NullPointerException("the file " + path + " does not exist in the classpath");
        }
        return URLDecoder.decode(url.getFile());
    }
}

Related

  1. getAbsolutePath(final Class clazz, final String relative)
  2. getApplicationPath(Class cls)
  3. getAppPath(Class clazz)
  4. getClass(String parentPath, String className)