1

I'm trying to insert an image into a HTML file from a java method with Eclipse. When I export the project into a Runnable jar, the image doesn't appear.

I searched a lot on the internet, and i did a ressource folder, but it doesn't work.

src
--test4
----IHM2.java
----test4.java
--res
----logo2.png

public class IHM2
{
private File htmlFile;
private FileWriter fw;
private BufferedWriter writer;

public IHM2() throws IOException {
    htmlFile = new File("IHM2.html");
    fw = new FileWriter("IHM2.html");
    writer = new BufferedWriter(fw);
}

public void HTML_opening() throws IOException
{

    writer.write("<html>");
    writer.write("<head>");
    writer.write("</head>");
    writer.write("<body>");
    writer.write("  <img src='res/logo2.png'>");

}

public void HTML_closing() throws IOException
{
    writer.write("</body>");
    writer.write("</html>");
    writer.close();
    Desktop.getDesktop().browse(htmlFile.toURI()); 
}
}

Any one please provide me an answer ?

SOLUTION : I put writer.write(" <img src='./logo2.png'>"); Because I looked into the .jar file and logo2.png was out of the folder test4.

johnywhite
  • 21
  • 1
  • 8

2 Answers2

0

Check inside the jar, do you have logo2.png?

If not, while exporting the jar from eclipse as runable jar, use the radio button "package required libraries into generated jar". If the res folder is a source folder then the png image will be copied into the root.

so instead of res/logo2.png you simply need to use logo2.png

Garry
  • 4,493
  • 3
  • 28
  • 48
0

Perhaps the best way to port your html with your jar file is to convert it into base64 data.

You can get your base64 of your image from the following link:

http://beta.base64-image.de/

Follow the instruction and get your image code. Once you uploaded your image, get the code and use it in your src="" tag.

Or else if you want to show the html, you must have the image next to it.

For more information you can check: Embedding Base64 Images

You can also find ways to convert your image directly from java using:

Java - Convert image to Base64

Finally, if you are interested to use fines in your resources, in somewhere else than your jar, you must save them into your hard disk, so they will be accessible by other apps, and then you can address it to open with any program.

Check this:

How to correctly get image from 'Resources' folder in NetBeans

Community
  • 1
  • 1
Soley
  • 1,716
  • 1
  • 19
  • 33
  • thank you for sharing this interesting way of inserting an image in a HTML file. At work uploading picture is forbidden due to the proxy server. But it's a nice to know that ;-) – johnywhite Aug 04 '15 at 12:03
  • You can use Java's. I use it and I don't like uploading myself as well. – Soley Aug 04 '15 at 15:28