gpt4 book ai didi

java - getClass().getResource(resourcePath) 在 Windows 上有效,在 Linux 上为空

转载 作者:IT王子 更新时间:2023-10-29 00:35:30 32 4
gpt4 key购买 nike

我有问题,这个电话

URL fileURL = getClass().getResource(resourcePath);

适用于 Windows (7 64b) 但不适用于返回 null 的 linux (Ubuntu 13.10 64b)。

为什么?文件在那里,字符串如下(相对路径)

String resourcePath = "/tut01/shaders/vertex_shader.glsl"

两个文件都在我家

编辑:该项目是新克隆的,我忘记清理和构建,对此感到抱歉..所以现在它找到了它们。然而,这很奇怪,因为即使我修改了,比方说,vertex_shader.glsl,我的程序将始终引用旧版本,每次我编辑它时,我都需要进行清理和构建才能看到变化......为什么?在 Windows 上我不必这样做..

最佳答案

您的资源路径以 / 开头,因此是绝对路径。如果您希望资源路径是相对的,您必须省略第一个 /

来自 Class.getResource(String name) 的 Javadoc:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form: modified_package_name/name where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

相对路径是相对于 getClass() 返回的类的路径。

一个例子:

package org.example;

public class MyClass {
public void foo() {
getClass().getResource("tut01/shaders/vertex_shader.glsl");
}
}

假设编译器将编译后的类文件写入/home/my-project/bin/org/example/MyClass.class

getClass().getResource("tut01/shaders/vertex_shader.glsl") 然后会在 /home/my-project/bin/org/example/tut01 中查找文件/shaders/vertex_shader.glsl.

关于java - getClass().getResource(resourcePath) 在 Windows 上有效,在 Linux 上为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21658061/

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