Close

Java Reflection - Class.getResource() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public URL getResource(String name)

Finds a resource with a given name.

Parameters:
name - name of the desired resource
Returns:
A URL object or null if no resource with this name is found

See Also: Different ways to load classpath resources in Java


Examples


package com.logicbig.example.clazz;

import java.net.URL;

public class GetResourceExample {

public static void main(String... args) {
Class<GetResourceAsStreamExample> c = GetResourceAsStreamExample.class;
//getting this class as resource
String resource = "/com/logicbig/example/clazz/GetResourceAsStreamExample.class";
URL url = c.getResource(resource);
System.out.println(url);
}
}

Output

file:/D:/examples/reflection-api/target/classes/com/logicbig/example/clazz/GetResourceAsStreamExample.class




See Also