gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:56:39 25 4
gpt4 key购买 nike

我有一个函数,我试图将文件加载到 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 还很陌生,所以我知道我做错了什么。

最佳答案

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

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

关于java - 如何从静态上下文中获取 getclass().getResource()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com