Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - JavaFx Images Path

I have a problem with my Java class. Actually the code is correctly, but if I click the run-button there's a exception caused of the path of the image.

static Image currentBackground = new Image("Snake/Images/background_options.png", true);

And the compiler's message is:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1100)
    at javafx.scene.image.Image.<init>(Image.java:624)
    at view.OptionsWindow.<clinit>(OptionsWindow.java:21)
    ... 3 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1092)
    ... 5 more

Process finished with exit code 1

Can anybody help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Image constructor is expecting the specification of a URL, not a file system path. Assuming you are bundling this image as part of your application, you will need to load that from the same place as your classes are loaded: probably a jar file in your final deployment, but perhaps from the file system during development.

The mechanism to get a URL representing a resource which is part of the application is to call getResource() on a Class or ClassLoader.

The exact way to do this depends on your project structure, which you haven't shown, but for example:

new Image(getClass().getResource("Snake/Images/background_options.png").toString(), true);

will load the image from a resource, specified relative to the current class, and

new Image(getClass().getClassLoader().getResource("Snake/Images/background_options.png").toString(), true);

will load the image from a resource specified relative to the class path.

In the event you pass a String that represents a relative URL (i.e. one with no scheme, such as file:, http:, or jar:), then the Image constructor will search on the class path for the resource. In other words

new Image("Snake/Images/background_options.png", true);

is equivalent to

new Image(getClass().getClassLoader().getResource("Snake/Images/background_options.png").toString(), true);

This seems slightly counter-intuitive (to me, at least), so I prefer to always specify a URL completely, or to retrieve one from getClass().getResource() or File.toURI().toURL() as appropriate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...