gpt4 book ai didi

java - 如何在 WebSphere 6.1 中正确获取类路径上资源的 URL?

转载 作者:搜寻专家 更新时间:2023-11-01 02:30:56 27 4
gpt4 key购买 nike

下面的代码在 Tomcat 中运行良好,但在 WebSphere 6.1 中调用 getResource(...) 返回 null。我试过同时使用 Thread.currentThread().getClassLoader() 和 MyClass.class.getClassLoader() - 都返回 null。

    URL url = null;
ClassLoader cl = MyClass.class.getClassLoader();
LOG.info("Using class's classloader.");

url = cl.getResource("resources/AConfigFile.xml");

if(url == null) {
throw new RuntimeException("The ClassLoader returned null for the URL of the " +
"the XML Document. This is definitely not right.");
}

...我也试过这个,没有运气...

   URL url = null;

url = MyClass.class.getResource("resources/AConfigFile.xml");

if(url == null) {
throw new RuntimeException("The ClassLoader returned null for the URL of the " +
"the XML Document. This is definitely not right.");
}

这是怎么回事?如何正确获取类路径上资源的 URL?

最佳答案

我猜不同之处在于 ClassLoader 的行为方式。您可以改用 Class 变体吗?我的课。 class.getResource() ?我们一直在 WebSphere 6.1 下使用 Class.getResourceAsStream()

或者尝试在您的资源路径前加上一个前导斜杠。

使用Class 变体,您的相对路径将在MyClass 包下的resources 子目录中查找。但 ClassLoader 变体可能不会。

关于java - 如何在 WebSphere 6.1 中正确获取类路径上资源的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507419/

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