Java - File writing/reading works on Windows but not Mac - java

I'm currently developing an application that I wish to work on both Windows and other operating systems.
I have run into a problem where the files that are being called from folders in my program are not appearing on macOS, but ARE appearing on Windows.
All of my files are being written and read the same way as this example GUI code:
public class GUI extends JFrame {
private JLabel imageLabel;
public GUI() {
this.setTitle("test");
this.setSize(500,349);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
imageLabel = new JLabel(new ImageIcon("assets/test.png"));
this.getContentPane().setBackground(new Color(20,50,250));
this.getContentPane().add(imageLabel);
this.setSize(500, 350);
}
}
On Windows, the GUI program looks like this:
On MacOS, the GUI program looks like this:
I would like to add that these are being run on the operating systems via a Runnable Jar file exported from Eclipse IDE. The version that is being used is JavaSE-1.8.

The problem line lies here:
imageLabel = new JLabel(new ImageIcon("assets/test.png"));
My Solution:
imageLabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("test.png")));
Explaination:
My original intention was to somehow incorporate the resources of the program itself to work together with the JAR executable. In my testing (I'm new with making executables), I found that nothing would work unless I had a separate folder "assets" that the user would also download with the jar file. While this solution did work for a little bit, some reading and writing seemed to be different on some machines.
In order for this to work, you must add the "assets" folder or whatever folder has your images inside of them to your Java Build Path.
From here, it works on all tested machines; Windows and MacOS.

Related

Trying to include an image in a .jar exported file

I'm creating a Java program with several windows (JFrame). One of them contains an image as a JLabel.
I have tried a lot to include the image in the .jar file exported from Eclipse , to allow the program to read them not only on my pc: however it doesn't work!
Basically, as soon as I press the button that should open the frame containing the image, nothing happens (this on the exported file, while on Eclipse it works perfectly).
I'm going to post the code, I state that I am just starting out as a programmer and I am trying to learn Java, but I am not too familiar with the subject yet.
Thanks to Anyone who will spend His time to make my request.
Richard
URL url = ClassLoader.getSystemResource("images/books.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel iconl = new JLabel(imageIcon); // the images has been saved in folder images, that is located in the bin folder

JPanel doesnt show up after generated as .jar java

I have a problem with a program i wrote. The Problem is, that when i compile and execute it from my programming environment, everything works and shows up fine, but after i generate a .jar out of it, the frame itself shows up, but the jpanel with icons inside the JTabbedPane doesn't. I used Images in some icons in the jpanel, in other jpanels where i didnt use icons, it works after creating a .jar. i think the problem is getting the icons from the folder where the .jar is in but i dont know how to do this better.
Already tried:
ImageIcon icon1 = new ImageIcon(getClass().getResource("refresh.png"));
JLabel label1 = new JLabel(icon1);
and
URL url = home.class.getResource("refresh.png");
ImageIcon icon1 = new ImageIcon(url);
JLabel label1 = new JLabel(icon1);
but nothing seems to work after the .jar is cretated
In programming environment:
As generated .jar:
I searched everywhere but i didn't find an answer to this. If you need some more code let me know.
Try reading the file from the resources folder like this:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("refresh.png").getFile());
ImageIcon icon = new ImageIcon(file.toURI().toURL());
This will load the file from your classpath. For this to work, you will need to copy the file into the standard "resources" directory as shown in this example. And yes, you really do want to use this standard maven convention for storing files your application needs. It simplifies moving your project from development to other environments like test or production.

JFrame design change after generating JAR file? Image missing

I have a little problem with Swing (or maybe netBeans), I was making a testing JFrame, I changed its background by an image with this line of code:
this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src/testdesign/BG.png")))));
And I added a JLabel with an Icon to the frame using netBeans.
The result when compiling the project (In the IDE) was:
All is fine.. But when I generate a JAR file, the result is:
I'm not a Java pro, so I think that the way I changed the background isn't the proper way, or maybe it's another thing.
You use incorrect way to load background image. In your code image location depends on project directory src. You should load image from classpath but file system. You Class.getResource method to fix it e.g.:
this.setContentPane(new JLabel(new ImageIcon(getClass().getResource(pathToImage))));

Executable jar doesn't show up the icons/images used in the application

While writing my application (used Eclipse as my IDE), I didn't realize the problem that my images would not be eventually bundled in the jar. Now after completing the project and creating the executable jar, I bumped into this problem. I have around 50 java classes, and I have used images/icons extensively in most of these classes (e.g. in menus, as JLabel icon, in buttons, as tab icons, etc). I have used the ImageIcon API in most of the cases.
One example:
JLabel myHeaderImage = new JLabel(new ImageIcon("images/myHeader.jpg"));
What's the best way to pull in the images in the jar. Do I need to change the way I have used the images in my project?
Use getResource():
JLabel myHeaderImage = new JLabel(new ImageIcon(getClass().getResource("/images/myHeader.jpg")));
Just put a full path to ur images, such as in my case:
ImageIcon icon = new ImageIcon("X:/ProjektyNetBeans/GUITest/src/data/world.png");
Then Clean and Build. File .jar will work fine. Good luck.

Simple ImageIcon issue

I can't get icons to appear on my button. I've copied this code from a book as I'm a beginner trying to grasp Java programming. Unfortunately I can't progress without getting this working, the other exercises are based on GUI images. I believe I've followed the exact steps, hopefully somebody can help!
Here is the simple code:
package Chapter13;
import javax.swing.*;
/** #author Chris */
public class TestButtonIcons extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new TestButtonIcons();
frame.setTitle("ButtonIcons");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public TestButtonIcons()
{
ImageIcon usIcon = new ImageIcon("image/usIcon.gif");
ImageIcon caIcon = new ImageIcon("image/caIcon.gif");
ImageIcon ukIcon = new ImageIcon("image/ukIcon.gif");
JButton jbt = new JButton("Click it", usIcon);
jbt.setPressedIcon(caIcon);
jbt.setRolloverIcon(ukIcon);
getContentPane().add(jbt);
}
}
File Hierachy:
As you can see I've copied the "image" file twice as an attempt to try and debug this. All the images are in the "image" folder despite the photos not showing them all.
you need to create the image folder outside of the source package when you run from the netbeans while when you run directly executing the jar then you need to put the executable jar file and image folder in same folder or same place
It seems a bit of a problem with BookExcercises; that is not a normal project.
A normal NetBeans project (you can see all in the Files tab to the right of the Projects tab): there is a src directory shown under Source Packages. Building a project fills a target directory. Now a build would put the .class files there and also copy your images. Run (project) would run with as class path the target directory. Run File however might not find the images in the target directory.
A solution depends on the BookExcercises project. You might create a new project for every excercise. Depends.
If you are using a command Prompt to run your program, then simply paste this image folder alongside your chapter13 package folder(which contains your TestButtonIcons.class).
Or if you using some IDE, check where your compiled stuff goes. Again paste your image folder alongside your chapter13 package folder (which contains your TestButtonIcons.class). That will do the needful to run the program as you are expecting.
Regards

Categories