classLoader.getResource(filename) 返回不存在的文件(返回null)

huangapple go评论72阅读模式
英文:

classLoader.getResource(filename) not returning existent file (returning null)

问题

The code you provided seems to be encountering an issue where the following line:

final File commaDelimited = new File(classLoader.getResource(filename).getFile());

is returning null. This issue might be related to the way you're trying to load the .json file. Ensure that the file path is correct and that the file is located in the expected directory. Double-check the file name and path, including any capitalization and spacing.

Additionally, when working with resources in a Java project, you should use the getResourceAsStream method instead of getResource for better compatibility, especially when your code is running from a JAR file or in different environments. Here's how you can modify the code:

InputStream inputStream = classLoader.getResourceAsStream(filename);
if (inputStream != null) {
    // Process the inputStream
    // For example, you can create a BufferedReader to read the content of the file.
} else {
    // Handle the case where the resource was not found
}

This approach is more robust and should help you avoid issues with resource loading. Make sure to handle any exceptions that may arise when reading the file.

英文:

I'm trying to load a .json file for testing purposes on a Beam pipeline.

The code looks like

...
public class ExtractCsvMessageTest {
  @Rule public final transient TestPipeline pipeline = TestPipeline.create();

  final String filepath = "com/project/functions/ExtractCsvMessageTest/";
  final String filename = filepath + "comma_delimited.json";
  final ClassLoader classLoader = getClass().getClassLoader();

  final File commaDelimited = new File(classLoader.getResource(filename).getFile());
...

After running the debugger, I can see the line throwing the error is:

final File commaDelimited = new File(classLoader.getResource(filename).getFile());

My path looks like

test
    ├── java
    │   └── com
    │       └── project
    │           ├── functions
    │           │   ├── ExtractCsvMessageTest.java
    │           └── transforms
    └── resources
        └── com
            └── project
                └── functions
                    └── ExtractCsvMessageTest
                        └── comma_delimited.json

There are similar questions on here but I can't find anything which solved this issue.

Also, I'm using VSCode and I just created the path from resources manually.

Finally, my .classpath is

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" output="bin/main" path="src/main/java">
		<attributes>
			<attribute name="gradle_scope" value="main"/>
			<attribute name="gradle_used_by_scope" value="main,test"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="src" output="bin/main" path="src/main/resources">
		<attributes>
			<attribute name="gradle_scope" value="main"/>
			<attribute name="gradle_used_by_scope" value="main,test"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
	<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
	<classpathentry kind="output" path="bin/default"/>
	<classpathentry kind="src" path="src/test/java" output="build/classes/test">
		<attributes>
			<attribute name="test" value="true" />
		</attributes>
	</classpathentry>
</classpath>

tl:dr

Why is

final File commaDelimited = new File(classLoader.getResource(filename).getFile());

returning null?

答案1

得分: 1

这是一个类路径问题,正如我所怀疑的那样。

我在VSCode中通过使用 Cmd + Shift + P 触发命令面板并选择 Java: Clean the Java language server workspace 来解决了这个问题。

然后,这更新了 .classpath 文件。

退出并重新启动VSCode后,一切都恢复正常了。

英文:

This was a classpath issue, as suspected.

I solved it in VSCode by triggering the command palette with Cmd + Shift + P and selecting

Java: Clean the Java language server workspace

This in turn updated the .classpath file.

After exitting and launching VSCode again, everything was working again.

huangapple
  • 本文由 发表于 2020年8月5日 22:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63267106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定