maven模块主要功能使用错误的文件路径(maven module main function using wrong filepath)

编程入门 行业动态 更新时间:2024-10-28 18:25:15
maven模块主要功能使用错误的文件路径(maven module main function using wrong filepath) java

我已经花了3个小时来解决这个愚蠢的问题,而且我已经看了遍历stackoverflow,似乎没有人有这个特定的答案(我能找到)。

我会尽量简明扼要地解释我的问题。 首先,我使用IntelliJ作为我的IDE。

这是我的文件夹结构:

Utilities |-----RandomFolder |-----RandomFolder `-----Java | `--MavenProjectFolder |----src/main/ | |----java/Main.java | `---resources/test.json `---pom.xml

请注意,此结构中的Random文件夹无关紧要。

在这个结构中,Main.java是一个包含public static void main()函数的简单文件,我在其中尝试读取test.json文件,如下所示: File jsonFile = new File("/test.json") 。

现在我知道,在maven编译我的项目之后,它应该将所有内容放在我的根文件路径中的resources文件夹中(除非我遗漏了一些东西)我认为我尝试在json文件中读取的语句应该没有问题,但我一直得到一个file not found exception 。

在尝试了一些其他的东西后,我试着最终将json文件放在Utilities文件夹的根目录中,因为它运行了System.out.println(System.getProperty("user.dir")); 似乎表明该项目正在使用Utilities作为根目录,不知何故似乎有效。

现在我的问题是,我究竟如何获得我的maven项目(它应该与包含它的父文件夹完全无关)从其自己的根目录中读取文件(它在编译项目时创建的结构) )?

我已经验证了test.json文件确实在项目编译时被复制到classes文件夹中,但是File jsonFile = new File("test.json")仍然继续给我一个file not found exception 。

I have literally spent 3 hours on this stupid problem, and I have looked all over stackoverflow and no one seems to have this specific answer(that I could find).

I will try to explain my problem as concisely as possible. First of all I am using IntelliJ as my IDE.

This is my folder structure:

Utilities |-----RandomFolder |-----RandomFolder `-----Java | `--MavenProjectFolder |----src/main/ | |----java/Main.java | `---resources/test.json `---pom.xml

Note that the Random folders in this structure don't matter.

In this structure, Main.java is a simple file containing a public static void main() function in which I try to read in the test.json file like so: File jsonFile = new File("/test.json").

Now I know that after maven compiles my project, it is supposed to place everything in the resources folder in my root file path so (unless I am missing something) I figured that statement where I try to read in the json file should have no problems, BUT I keep getting a file not found exception.

After trying some miscellaneous things I tried to eventually place the json file in the root of the Utilities folder since running System.out.println(System.getProperty("user.dir")); seemed to indicate that the project is using Utilities as the root directory, somehow that seems to work.

Now my question is, how on earth do I get my maven project (which should have absolutely nothing to do with the parent folder(s) containing it) to read files from its own root directory (the structure it creates when it compiles the project)?

I have already verified that the test.json file does in fact get copied into the classes folder when the project compiles but File jsonFile = new File("test.json") still continues to give me a file not found exception.

最满意答案

我找到了自己问题的答案: File jsonFile = new File(Main.class.getResource("test.json").toURI()) 似乎从maven编译的类路径中获取文件。

I found an answer to my own question: File jsonFile = new File(Main.class.getResource("test.json").toURI()) seems to grab the file from the maven compiled class path.

更多推荐

本文发布于:2023-07-14 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主要功能   路径   模块   错误   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!