Java resource loading explained.

本文深入探讨了Java资源加载机制,解释了位置独立性、绝对路径与相对路径的区别,以及ClassLoader与Class如何处理资源名称。通过示例代码演示了资源加载的不同方式及其在不同环境下的表现差异。

Understanding how Java finds resources is important. Resource loading in Java is “location independent”, Java also differentiates between absolute and relative resource names. And these names are processed by Java ResourceLoader differently. That last little issue may cause problems, especially because the “wrong” resource names really work under some circumstances.

1

How java locates resources

Java loads resources from the “environment”, in many cases it uses all jars in Classpath to retrieve the resource. Resource loading in java is called location independent because it is not relevant where your code is running, it just needs correct environment to find the resources.

Given all jars and paths in Classpath, java will search relatively to each tone to find the resource you specified. You specify resources using resource names.

2

Absolute and relative resource names

Resources are referred using resource name:
getResourceAsStream("/path/resource.xml");

“/path/resource.xml” is the resource name here.

The resource name can be:

  • absolute like “/path/resource.xml”;
  • relative like “path/resource.xml”;

 
Relative means, relative to the location, where the method was called. The path will be appended if needed.
Absolute will be used as is, only first / will be removed before the search.

Example:

package my.location;
 
class ResourceFinder {
...
public void findResources(){
   InputStream stream1 =
getClass().getResourceAsStream("/path/resource.xml");
   InputStream stream2 =
getClass().getResourceAsStream("path/resource.xml");
}
...
}

 

➕ In this case, stream1 will get the resource, located in classpath path/resource.xml.

➕ stream2 will get the resource, located in classpath my/location/path/resource.xml (Relative to package where the search was initiated).

3

ClassLoader and Class apply resource names differently

There are ClassLoader.getResource() and Class.getResource() and they work differently!!!

java docu:

The methods in ClassLoader use the given String as the name of the
resource without applying any absolute/relative transformation (cf. the
methods in Class). The name should not have a leading “/”.

So, to extend our example:

package my.location;
 
class ResourceFinder {
...
public void findResources(){
   InputStream stream1 =
getClass().getResourceAsStream("/path/resource.xml");
   InputStream stream2 =
getClass().getResourceAsStream("path/resource.xml");
   InputStream stream3 =
getClass().getClassLoader().getResourceAsStream("path/resource.xml");
   InputStream stream4 =
getClass().getClassLoader().getResourceAsStream("/path/resource.xml");
 
}
...
}

 

➕ stream3 will become resource in classpath under path/resource.xml.

⚠ stream4 resource name /path/resource.xml is wrong!

4

Where it is important. Example

I was assessing Maven3 against Maven2 build. Same big project, just call m3.

Following code has worked well in my previous environment:

InputStream exportFileInputStream =
getClass().getClassLoader().getResourceAsStream("/com/thinkplexx/lang/de/general.xml");

 

But after I changed the environment (maven2 build -> maven3 build) the general.xml was missing. I know that resource is there.

Following worked:

InputStream exportFileInputStream =
getClass().getClassLoader().getResourceAsStream("com/thinkplexx/lang/de/general.xml");

 
It is a speculation as of how the first snippet could have worked, but it deed. Going to new clean environment triggered the bug.

Links

1) http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/guide/resources/resources.html
2) http://stackoverflow.com/questions/3238562/getresourceasstream-fails-under-new-environment
3) http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/ClassLoader.html
4) http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/Class.html

### Explained Variance Score 的定义与用法 Explained Variance Score 是一种用于评估回归模型性能的指标。它衡量的是预测值相对于真实值的变化程度能够被模型解释的比例。该分数的最佳值为 1.0,表示所有的变化都可以由模型完全捕捉到;而较低的值则表明模型未能很好地捕获数据中的变化。 具体来说,Explained Variance Score 可以通过以下公式计算: \[ \text{explained\_variance}(y, \hat{y}) = 1 - \frac{\text{Var}\{ y - \hat{y} \}}{\text{Var}\{y\}} \] 其中 \(y\) 表示真实的观测值,\(\hat{y}\) 表示预测值,\(\text{Var}\) 表示方差[^4]。 #### Python 实现代码 以下是使用 `sklearn` 库实现 Explained Variance Score 的简单例子: ```python from sklearn.metrics import explained_variance_score import numpy as np # 假设这是我们的实际目标值和预测值 true_values = np.array([3, -0.5, 2, 7]) predicted_values = np.array([2.5, 0.0, 2, 8]) # 计算 Explained Variance Score score = explained_variance_score(true_values, predicted_values) print(f"Explained Variance Score: {score}") ``` 在这个例子中,`explained_variance_score` 函数会返回一个介于负无穷大和 1 之间的数值。如果得分为 1,则说明模型完美地拟合了数据;得分越低,则说明模型的效果越差。 #### 解释与其他度量的关系 尽管 Pearson 相关系数可以用来粗略估计变量的重要性[^2],但它仅适用于线性关系的情况。相比之下,Explained Variance Score 更加通用,因为它考虑到了残差的整体分布情况以及它们对方差的影响。因此,在复杂的数据建模场景下,尤其是涉及非线性模式时,推荐优先采用此评分标准作为评价依据之一。 ### 注意事项 需要注意的一点是,虽然高分通常意味着较好的表现,但是单独依赖某一项指标可能会忽略其他潜在的重要因素。所以在实践中往往需要结合多个不同的误差测量手段来进行全面分析。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值