getClassLoader().getResource(filepath)返回空指针 [英] getClassLoader().getResource(filepath) returns a null pointer

查看:423
本文介绍了getClassLoader().getResource(filepath)返回空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种方法为研究项目动态生成XML文件,将它们放入从文件路径读取的加载器中,但我对加载器的处理方式没有任何控制(否则我会通过内部XML表示而不是临时文件),我正在使用以下代码来保存文件:

I'm using a method to generate XML files dynamically for a research project, they get put into a loader that reads from a file path, I don't have any control over how the loader handles things (otherwise I'd pass the internal XML representation instead of monkeying with temp files), I'm using this code to save the file:

File outputs = File.createTempFile("lvlFile", ".tmp.xml");

FileWriter fw = new FileWriter(outputs);

fw.write(el.asXML());

fw.close();

// filenames is my list of file paths which gets returned and passed around

filenames.add(outputs.getAbsolutePath());

现在,我确定有问题的文件已直接写入.如果我打印outputs.getAbsolutePath()并通过终端导航到那里以检查文件,则所有内容都会正确生成并写入,因此文件系统上的所有内容都是正确的.但是,此代码:

Now, I'm sure that the file in question is written to directly. If I print outputs.getAbsolutePath() and navigate there via terminal to check the files, everything is generated and written properly, so everything is correct on the filesystem. However, this code:

URL url = this.getClass().getClassLoader().getResource(_levelFile);

其中_levelFile是我上面生成的文件名之一,导致url为null.路径没有损坏或任何东西,打印验证_levelFile指向正确的路径.相同的代码已成功用于其他文件.此外,该错误似乎与我是否使用getPath(),getCanonicalPath()或getAbsolutePath()无关,进一步设置outputs.isReadable(true)无效.

Where _levelFile is one of my filenames generated above, causes url to be null. The path isn't getting corrupted or anything, printing verifies that _levelFile points to the correct path. The same code has succeeded for other files. Further, the bug doesn't seem related to whether or not I use getPath(), getCanonicalPath(), or getAbsolutePath(), further setting outputs.isReadable(true) doesn't do anything.

有什么想法吗?请不要提供Url url =结构的替代方法,我对此代码没有任何控制*,我有义务更改我的代码,以便正确设置url.

Any ideas? Please don't offer alternatives to the Url url = structure, I don't have any control over this code*, I'm obligated to change my code so that the url is set correctly.

(*),即使当前代码在所有其他情况下都能成功,至少也无需花费大量精力来重写我正在使用的框架的很大一部分.

(*) At least without SIGNIFICANT effort rewriting a large section of the framework I'm working with, even though the current code succeeds in all other cases.

同样,我无法使用URL代码的替代方式,它是无法触摸的加载程序的一部分.另外,即使我将临时文件的路径设置为成功加载文件所来自的目录,加载也会失败.

Again, I can't use an alternative to the URL code, it's part of a loader that I can't touch. Also, the loading fails even if I set the path of the temp file to the same directory that my successfully loaded files come from.

推荐答案

我假设ClassLoader只在类路径中查找资源-可能不包含/tmp.我不确定它是否真正支持绝对路径名.它可能只是将它们解释为相对于单个类路径的根.

I assume that the ClassLoader will only look for resources within the class path - which probably doesn't include /tmp. I'm not sure if it actually supports absolute path names. It might just interpret them as relative to the root of the individual class path.

如何使用_levelFile.toURI().toURL()代替?

这篇关于getClassLoader().getResource(filepath)返回空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆