How to get getclass().getResource() from a static context?
Asked Answered
C

6

46

I have a function where I am trying to load a file to a URL object, because the example project said so.

public class SecureFTP {

    public static void main(String[] args) throws IOException , ClassNotFoundException, SQLException , JSchException, SftpException{
        File file = new File("/home/xxxxx/.ssh/authorized_keys");
        URL keyFileURL = this.getClass().getClassLoader().getResource(file);

I tried using SecureFTP.class.getResource, but it still could not compile it.

I am fairly new to Java, so I know I am doing something wrong.

Compass answered 2/12, 2011 at 20:3 Comment(0)
H
9

It can't compile because getResource takes a resource name (a String, and not a File) as parameter, in order to load a resource using the class loading mechanism (from the classpath). Using it with a File makes no sense. If you want to open a file, just use a FileInputStream or a FileReader.

See http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29, and include the compiler error message next time you have such a question.

Harem answered 2/12, 2011 at 20:9 Comment(0)
P
74

The main method is a static method, so trying to access this (= the current Object) will not work. You can replace that line by

URL keyFileURL = SecureFTP.class.getClassLoader().getResource("/home/xxxxx/.ssh/authorized_keys");
Padre answered 2/12, 2011 at 20:5 Comment(3)
That's precisely what the OP tried. Read the question more carfully. The problem is that he's passing a File rather than a STring to the method.Harem
You are right. I corrected my answer. However, the OP tried SecureFTP.class.getResource, without the getClassLoader callPadre
Your method will not work: getResource doesn't load resources from the file system, and resource names don't start with a /. Read my answer.Harem
S
28

From: How to call getClass() from a static method in Java?

Just use TheClassName.class instead of getClass().

Scolex answered 30/4, 2015 at 13:26 Comment(0)
P
22

Old question but this hasn't been said yet. You can do this from a static context:

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
classLoader.getResource("filename");
Pluralize answered 2/2, 2017 at 19:38 Comment(1)
This answer worked for me. (I've even upvoted it). However, when tried to run mvn exec:java it couldn't locate the resource. Using the other answers here (SecureFTP.class.getClassLoader().getResource) solved it.Encratis
H
9

It can't compile because getResource takes a resource name (a String, and not a File) as parameter, in order to load a resource using the class loading mechanism (from the classpath). Using it with a File makes no sense. If you want to open a file, just use a FileInputStream or a FileReader.

See http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29, and include the compiler error message next time you have such a question.

Harem answered 2/12, 2011 at 20:9 Comment(0)
T
5
SecureFTP.class.getClassLoader().getResource(<<your resource name>>); 

Should do the trick!

Thwack answered 28/5, 2014 at 14:11 Comment(0)
P
0

Do it this way so that it works EITHER from a static method or an instance method:

public static String loadTestFile(String fileName) {
    File file = FileUtils.getFile("src", "test", "resources", fileName);
    try {
        return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    } catch (IOException e) {
        log.error("Error loading test file: " + fileName, e);
        return StringUtils.EMPTY;
    }
}
Passementerie answered 25/5, 2022 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.