简体   繁体   English

getResource()在Eclipse中(不在JAR文件中)返回null

[英]getResource() return null in Eclipse not in JAR file

Am developing application in spring boot.I have the two packages inside src/main/java 1. com.project.test and 2. wsdl . 我正在春季启动中开发应用程序。我在src / main / java 1. com.project.test2. wsdl中包含两个软件包。 Inside first package I have spring boot main class. 在第一个程序包中,我有spring boot主类。 inside second package I have test.wsdl file. 在第二个包中,我有test.wsdl文件。 I try to load the test.wsdl file in main class using below code 我尝试使用下面的代码在主类中加载test.wsdl文件

URL wsdl = MainClass.class.getClassLoader().getResource(/wsdl/test.wsdl); system.out.println("wsdl: "+wsdl);

It return null while run in Eclipse. 在Eclipse中运行时返回null。 When build application as jar and run application using java -jar app.jar . 将应用程序构建为jar并使用java -jar app.jar运行应用程序时。 It return correct path. 它返回正确的路径。 Why it return null in Eclipse. 为什么它在Eclipse中返回null。 But when I run below code without prefix '/' like below. 但是当我在下面没有前缀'/'的代码下运行时,如下所示。 Its working fine in both Eclipse and JAR 在Eclipse和JAR中都可以正常工作

URL wsdl = MainClass.class.getClassLoader().getResource(wsdl/test.wsdl); system.out.println("wsdl: "+wsdl);

But my requirement is to load resource using path /wsdl/test.wsdl 但我的要求是使用路径/wsdl/test.wsdl加载资源

You need to move wsdl/test.wsdl inside src/main/resources in order to load resource. 您需要在src/main/resources中移动wsdl/test.wsdl以便加载资源。

When you package you app.jar , wsdl/test.wsdl goes into root path inside jar file, so .getClassLoader().getResource("wsdl/test.wsdl") works as expected. 打包app.jarwsdl/test.wsdl进入jar文件内的根路径,因此.getClassLoader().getResource("wsdl/test.wsdl")可以正常工作。

To quote relevant Java documentation (emphasis mine): 引用相关的Java文档(重点是我的):

Resource Names 资源名称

A common convention for the name of a resource used by a class is to use the fully qualified name of the package of the class, but convert all periods ( . ) to slashes ( / ), and add a resource name of the form name.extension . 类使用的资源名称的常见约定是使用该类的程序包的完全限定名称,但是将所有句点( . )转换为斜杠( / ),并添加表单name.extension的资源name.extension To support this, and to simplify handling the details of system classes (for which getClassLoader returns null ), the class Class provides two convenience methods that call the appropriate methods in ClassLoader . 为了支持这一点,并简化系统类的详细信息( getClassLoader返回null ),类Class提供了两个方便的方法,这些方法调用ClassLoader的相应方法。

The resource name given to a Class method may have an initial starting "/" that identifies it as an "absolute" name. 分配给Class方法的资源名称可能具有一个初始的开始“ /”,将其标识为“绝对”名称。 Resource names that do not start with a "/" are "relative". 不以“ /”开头的资源名称是“相对”。

Absolute names are stripped of their starting "/" and are passed, without any further modification, to the appropriate ClassLoader method to locate the resource. 绝对名称会从其开头的“ /”中删除,并且无需进行任何进一步修改即可传递给适当的ClassLoader方法以查找资源。 Relative names are modified according to the convention described previously and then are passed to a ClassLoader method. 相对名称根据前面描述的约定进行了修改,然后传递给ClassLoader方法。

Link: Accessing Resources 链接: 访问资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM