0

I've been trying to load an image into an ImageView, and it works fine when I run it as a standalone app, but when I try to run it in the browser or as webstart it doesn't get shown.

I've tried:

ivFoto = new ImageView(new Image("file:/C:/Users/Carlos/Desktop/4fbzW.jpg"));

or

ivFoto = new ImageView(new Image("file:\\C:\\Users\\Carlos\\Desktop\\4fbzW.jpg"));

or

ivFoto = new ImageView("file:///C:/Users/Carlos/Desktop/4fbzW.jpg");

If anyone has any idea of what I'm doing wrong here, I would really appreciate the help!

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
cdn34
  • 107
  • 1
  • 8

1 Answers1

0

Package your image (4fbzW.jpg) in your application jar file (in the same location as the class loading the image) and reference it as a resource:

ivFoto = new ImageView(
  new Image(
    this.getClass().getResource("4fbzW.jpg").toExternalForm()
  )
);

There are a couple of things wrong with the image references in your question:

  1. Hardcoding the file: protocol.

    A JavaFX application will usually be packaged as a jar file, so resources for the application should be accessed using the jar protocol. By using the getResource method, the resource will be fetched by the same protocol used to load the class referencing the resource, thus it will automatically use the correct protocol to find the resource and you don't need to worry about this detail.

  2. Referring to local resources on your development machine.

    Webstart and browser embedded application deployment scenarios are primarily used to reference applications which are delivered over a network to a remote client. The user of the client machine could have any username or be using any supported OS, so they will be unlikely to have a file located at: C:/Users/Carlos/Desktop. By packaging the image with your application and getting it from a location relative to your application's code, then the image can always be found consistently no matter where it is deployed.

  3. File based access violates the security sandbox for WebStart and browser embedded apps

    These kind of deployment modes run applications with restricted privileges to prevent malicious applications from doing bad things (like deleting all users files or sending them to a foreign intelligence service). To allow applications to run with elevated security privileges under these deployment modes, you need to sign the applications and the user needs to explicitly trust the software provided by you. In the particular case of your app, I would not recommend signing, but instead package the resources you require with the app so that you and your users do not need to undergo the inconvenience of a signed app.


See also: Where does javafx.scene.image.Image("flower.png") look for flower.png?

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406