简体   繁体   English

在maven项目中我如何指定文件的相对路径

[英]In maven project how i can specify a relative path to file

In javafx project which create without maven i specify relative path to .css file like 在没有maven创建的javafx项目中,我指定了.css文件的相对路径

Main.class.getResource("login.css").toExternalForm() Main.class.getResource( “login.css”)。toExternalForm()

and it's work.But when i create maven project and use this path i got a runtime exception, but if i use path like 它是工作。但是,当我创建maven项目并使用此路径时,我得到了一个运行时异常,但如果我使用路径像

./main/java/com/sowl/addStudent.css ./main/java/com/sowl/addStudent.css

or other like this i have exception about resource not found. 或者像这样的其他我没有找到资源的例外。 And i tried path use Thred 我试过路径使用Thred

Thread.currentThread().getContextClassLoader().getResource("addStudent.css").toExternalForm() Thread.currentThread()。getContextClassLoader()。的getResource( “addStudent.css”)。toExternalForm()

and again have runtime exception.How i can specify path for my javafx.css.StyleManager. 并再次有运行时异常。如何为我的javafx.css.StyleManager指定路径。 In advance,thx 提前,thx

In Maven projects, resources go to src/main/resources , not src/main/java by default. 在Maven项目中,资源默认转到src/main/resources ,而不是src/main/java

So if you want to call Main.class.getResource("login.css") (relative path), then login.css has to be in the same package as Main, but in the resources directory. 因此,如果要调用Main.class.getResource("login.css") (相对路径),则login.css必须与Main位于同一个包中,但必须位于resources目录中。

If you would call someClassLoader.getResource("addStudent.css") or someClass.getResource("/addStudent.css") (absolute path), then addStudent.css has to be directly in src/main/resources . 如果你要调用someClassLoader.getResource("addStudent.css")someClass.getResource("/addStudent.css") (绝对路径),那么addStudent.css必须直接在src/main/resources

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

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