如何从静态上下文中获取 getclass().getResource() ?

发布于 2024-12-19 01:59:47 字数 481 浏览 6 评论 0原文

我有一个函数,我试图将文件加载到 URL 对象,因为示例项目是这么说的。

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

我尝试使用SecureFTP.class.getResource,但仍然无法编译。

我对 Java 相当陌生,所以我知道我做错了什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨小沫ゞ 2024-12-26 01:59:47

main 方法是静态方法,因此尝试访问 this(= 当前对象)将不起作用。
您可以将该行替换为

URL keyFileURL = SecureFTP.class.getClassLoader().getResource("/home/xxxxx/.ssh/authorized_keys");

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");
风柔一江水 2024-12-26 01:59:47

来自:如何从静态方法调用 getClass()在Java中?

只需使用 TheClassName.class 而不是 getClass()。

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

Just use TheClassName.class instead of getClass().

谁的年少不轻狂 2024-12-26 01:59:47

老问题,但这还没有说。您可以从静态上下文中执行此操作:

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
classLoader.getResource("filename");

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

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
classLoader.getResource("filename");
漫漫岁月 2024-12-26 01:59:47

它无法编译,因为 getResource 采用资源名称(String,而不是 File)作为参数,以便加载资源使用类加载机制(从类路径)。将其与 File 一起使用是没有意义的。如果您想打开文件,只需使用 FileInputStreamFileReader 即可。

请参阅 http: //docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29,并在下次出现此类错误时包含编译器错误消息问题。

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.

娇女薄笑 2024-12-26 01:59:47
SecureFTP.class.getClassLoader().getResource(<<your resource name>>); 

应该做的伎俩!

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

Should do the trick!

徒留西风 2024-12-26 01:59:47

这样做,以便它可以从静态方法或实例方法工作:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文