Here you can find the source of getAbsolutePath(Class theClass, String path)
Parameter | Description |
---|---|
object | The object which classloader is used to retrieve the resource |
path | The relative path of the file |
Parameter | Description |
---|---|
NullPointerException | If the file is not in the classpath |
public static String getAbsolutePath(Class theClass, String path) throws NullPointerException
//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()); } }