Jump to content

(JavaFX) Image object path being weird.

Go to solution Solved by Biohazard777,
On 11/17/2023 at 1:32 AM, Kiyullo said:
Image icon = new Image("purp.png");

Isn't that supposed to be URI?
Like so:

Image icon = new Image("file:purp.png");

Try that.

I'm trying out JavaFX and when I instantiate an Image object it throws an IllegalArgumentException exception: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1120)

 

I assume the image path isn't working, but I'm doing exactly what's shown in a video where it works properly. If the image file (named purp.png) is in my src directory, I pass "purp.png". If I put it in a resources subdirectory I pass the path.

 

Tried in a brand new JavaFX project and it gives the same error:

 

package com.example.demo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
	@Override
	public void start(Stage stage) throws IOException {
		FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
		Scene scene = new Scene(fxmlLoader.load(), 320, 240);
		
		// Error occurs when instantiating Image.
		Image icon = new Image("purp.png");
		stage.getIcons().add(icon);
		
		stage.setTitle("Hello!");
		stage.setScene(scene);
		stage.show();
	}

	public static void main(String[] args) {
		launch();
	}
}

 

Anyone know why it's not working?

Java Trainee (Novice)

Thanks in advance~

Link to comment
https://linustechtips.com/topic/1541166-javafx-image-object-path-being-weird/
Share on other sites

Link to post
Share on other sites

21 hours ago, Kiyullo said:

If the image file (named purp.png) is in my src directory, I pass "purp.png"

Put the image in the root project folder, not the src folder.

Main System (Byarlant): Ryzen 9 5950X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 32GB G.Skill DDR4 3600MT/s CL16 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 990 EVO Plus 4TB | Corsair RM750X | StarTech 4× USB 3.0 Card | Realtek RTL8127 10G NIC | Hyte Y60 Case | Dell U3415W Monitor | Keychron K12 Blue (RGB backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB DDR4 3200MT/s (soldered) | Vega II 384SP Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi | Asus 2.5G USB NIC | Asus ProArt PA278QV | Keychron K4 Brown (white backlight)

 

Proxmox Server (Veda): Ryzen 7 3800XT | ASRock Rack X470D4U | Corsair H80i v2 | 128GB Micron DDR4 ECC 3200MT/s | 2× Samsung PM963a 960GB SSD / 4× WD 10TB / 4× Seagate 14TB Exos / 4× Micron MX500 2TB / 8× WD 12TB (custom external SAS enclosure) | Seasonic Prime Fanless 500W | Intel X550-T2 10G NIC | LSI 9300-8i HBA | Adaptec 82885T SAS Expander | Fractal Design Node 804 Case

 

Proxmox Server (La Vie en Rose)GMKtec Mini PC | Ryzen 7 5700U | 32GB Lexar DDR4 (SODIMM) | Vega II 512SP Graphics | Lexar 1TB 610 Pro SSD | 2× Realtek 8125 2.5G NICs


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | TrendNet (AQC107) 10G NIC | LG WH14NS40 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Workbench (Doven Wolf): Lenovo m715q | Ryzen Pro 3 2200GE | 16GB Crucial DDR4 3200MT/s (SODIMM) | Vega 8 Graphics | SKHynix (OEM) 256GB NVMe SSD | uni 2.5G USB NIC | HDMI add-in module

 

Network:

Spoiler
                       ┌─────────────── Office/Rack ───────────────────────────────────────────────┐
Google Fiber Webpass ── Cloud Gateway Max ══╦═ Pro XG 8 ══╦═ Flex 2.5-8 ══╦═ Doven Wolf
                      La Vie en Rose (DNS) ═╬═ Narrative  ╠═ Veda-NAS     ╠═ La Vie en Rose (vmbr)
                                Veda (DNS) ─┘             ╠═ Veda (vmbr)  ├─ Ptolemy (vmbr)
╔═════════════════════════════════════════════════════════╩═ Ptolemy-NAS  ├─ Veda (Mgmt)
║   ┌ Closet ┐      ┌───────── Bedroom ─────────┐                         └─ Veda (IPMI)
╚═══ Flex XG ══╦╤═══ Flex XG ══╤╦═ Byarlant
       (PoE)   ║│              │╠═ Narrative 
Kitchen Jack ══╣└─ Dual PoE ┐  │╚═ Jesta Cannon*
   (Testing)   ║┌─ Injector ┘  └── Work Laptop
     Bedroom ══╝│        ┌─────── Media Center ────────────────────────────┐
     Jack #2    └──────── Switch 8 ────────────┬─ nanoHD Access Point (PoE)
Notes:                                         ├─ Sony PlayStation 4 
─── is Gigabit / ═══ is Multi-Gigabit          ├─ Pioneer VSX-S520
* = cable passed from Bedroom to Media Center  └─ Sony XR65A80K (Google TV)
Link to post
Share on other sites

3 hours ago, AbydosOne said:

Put the image in the root project folder, not the src folder.

Tried that. Doesn't work. Also tried putting it in a subdirectory inside of the resources folder and adding the path. Also doesn't work.

 

There has to be some underlying issue that I just don't know about.

Java Trainee (Novice)

Thanks in advance~

Link to post
Share on other sites

10 hours ago, Biohazard777 said:

Isn't that supposed to be URI?
Like so:

Image icon = new Image("file:purp.png");

Try that.

Wow that actually worked. I can use the image. 👍

Now even if the specified image does not exist in the project, the object can still instantiate without an error.

It's very strange since in the video it worked normally with the code I was following along with (not the same code used in the demo code from the OP):

 

Maybe they changed how JavaFX works or something.

 

EDIT:

 

Also it says URL, not URI:

    public Image(@NamedArg("url") String var1) {
        this(validateUrl(var1), (InputStream)null, 0.0, 0.0, false, false, false);
        this.initialize((Object)null);
    }

 

Java Trainee (Novice)

Thanks in advance~

Link to post
Share on other sites

16 hours ago, Biohazard777 said:

Isn't that supposed to be URI?
Like so:

Image icon = new Image("file:purp.png");

Try that.

I tried putting the image in a subdirectory named images in the resources folder. I tried some paths but couldn't get it wo work. What path should I use if it's in resources/images?

 

EDIT:

I figured it out. Kind of. Instead of only putting the path for resources and images, I needed the entire path from the C drive.

That's a bit annoying since different PCs will probably have different paths. How can I keep it in the resource subdirectories while avoiding this issue?

Java Trainee (Novice)

Thanks in advance~

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×