在Maven/Java项目中创建资源 [英] Creating resources in a maven/java project

查看:67
本文介绍了在Maven/Java项目中创建资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建资源,使其位于项目的资源文件夹中? 在下面的内容中,"test.txt"是我要创建的文件,但是variable:url为null,因此无法获得我要创建的文件的路径.

How do I create a resource so that it is located in the resource folder of my project? In the following, "test.txt" is a file I want to create, but the variable:url is null, so I can't get a path to the file I want to create.

    URL url= HashArray.class.getResource("test.txt");
    File file = new File(url.toURI());

该资源可能需要位于资源文件夹中,因为我需要在打包阶段将其与代码捆绑在一起.

The resource probably needs to be located in the resources folder because I need to bundle it with the code in the packaging phase.

推荐答案

这不起作用,因为资源不是运行时位置.这是源位置.如果您尝试放置文件并运行程序,则url将不再为null.但这将指向您的target/classes文件夹(好的情况很多,具体取决于您启动它的方式).但是,在大多数情况下,它不再是资源了.

This can't work, as resources is not a runtime location. It's a source location. If you try to put a file, and run your program the url wouldn't be null anymore. But it would point to your target/classes folder (ok there are quite a number of possiblities, depending how exactly you start it) However, in most cases it wouldn't be resources anymore...

这篇关于在Maven/Java项目中创建资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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