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.

6 Answers 6

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");
3
  • 2
    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.
    – JB Nizet
    Commented Dec 2, 2011 at 20:10
  • You are right. I corrected my answer. However, the OP tried SecureFTP.class.getResource, without the getClassLoader call
    – Robin
    Commented Dec 2, 2011 at 20:13
  • 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.
    – JB Nizet
    Commented Dec 2, 2011 at 20:21
28

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

Just use TheClassName.class instead of getClass().

23

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

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
classLoader.getResource("filename");
1
  • 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.
    – Zvika
    Commented Oct 19, 2021 at 8:00
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.

5
SecureFTP.class.getClassLoader().getResource(<<your resource name>>); 

Should do the trick!

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.