从Java访问Gradle资源 [英] Accessing gradle resources from Java

查看:201
本文介绍了从Java访问Gradle资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个结构的基于gradle的java项目

 
├──myproject
│├──src
│| └──主
│| ├──java
│| └──资源
│| └──myresource.xml
| ├──建立
| | ├──课程
| | | └──主
│| | └──myresource.xml
| | ├──resources

我试图访问资源中的某些文件 ClassLoader.getSystemClassLoader()。getResoure(/ myresource.xml);使用ClassLoader的文件夹,如下所示:

  

但它找不到该文件。



我发现访问这些文件的唯一方法是通过探索项目的已知结构

  Path resourcesPath = FileSystems。 getDefault()。getPath(System.getProperty(user.dir),/ src / main / resources /); 

任何想法我做错了什么?

解决方案

好吧,看来我的困难来自另一个问题(资源没有被复制到适当的地方)。一旦我解决了这个问题,ClassLoader就能够使用以下两种形式之一找到我的资源:

.getResource( ./ myresource.xml);

ClassLoader.getSystemClassLoader()。getResource(myresource.xml);


I have a gradle-based java project with this structure

.
├── myproject
│      ├── src
│      |    └── main
│      |         ├── java
│      |         └── resources
│      |               └── myresource.xml
|      ├── build
|      |     ├── classes
|      |     |    └── main
│      |     |         └── myresource.xml
|      |     ├── resources

I'm trying to access some files in the resources folder using a ClassLoader, like this

ClassLoader.getSystemClassLoader().getResoure("/myresource.xml");

but it does not find the file.

The only way I have found to access those files is by exploring the known structure of the project

Path resourcesPath= FileSystems.getDefault().getPath(System.getProperty("user.dir"), "/src/main/resources/");

Any idea on what am I doing wrong?

解决方案

Well, it seems my difficulties came from another problem (resources not being copied to the proper places). Once I solved that problem, the ClassLoader was able to find my resources using either of these two forms:

ClassLoader.getSystemClassLoader().getResource("./myresource.xml");

ClassLoader.getSystemClassLoader().getResource("myresource.xml");

这篇关于从Java访问Gradle资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆