This is my first question ever in this site, but I've been checking this website since a month ago or so and it helped me a lot. I'm brand new to any kind of developing and I started with Java. Started 2 months ago so please, be super specific in the answer since I'm so so newbie. Thank you very much!
The problem I actually have is that I've been developing an application for my girlfriend that consists in a switch case with 144 case where each case got an "ImageIcon" set to label and there's a textfield where you have to write what kind of plant it is (It's for a plants exam). 1 case looks exactly like this:
ImageIcon img = new ImageIcon("images\\Tuber melanosporum.png");
ImageIcon icon = new ImageIcon(img.getImage().getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_DEFAULT));
jLabel2.setIcon(icon);
solucio = "Tuber melanosporum";
break;
She has to write "Tuber melanosporum" to suceed. Everything works, I've got a random number from 1 to 144 that makes the application to send random images each with their "solution".
PROBLEM 1: (SOLVED) The problem comes when I try to Compile and Distribute it. When compiled, I can't even execute my .jar. I've been looking here for questions with my problem, and I was told to check if Java was the default programm to open .jar, I ran a programm called "Jarfix", etc. But when I double click it, it ignores me. The only way to execute it from my PC is running the cmd command on the console. When I try to doubleclick it now, after a lot of changes, it says that cannot find the main class (My app, hasn't got a main class, It's only a jForm, I had another one with mainclass and jForm but I had the same error). I've seen answers about manifests and so, but seems that won't work for me... I've tried to to set the jForm as main class at Project Properties/Run.
My manifest in the .jar looks like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.1
Created-By: 1.7.0_51-b13 (Oracle Corporation)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: visuframe.visuframe //My only class (jFrame) is visuframe, inside package visuframe so I guess this is correct
SOLUTION: After several JRE reinstalls, I decided to reinstall all my JDK (Which includes JRE and Netbeans) and after that reinstall, it worked. I was running Netbeans 7.2 and JDK7 and JRE7 update 51. Now I'm running Netbeans 8.0 and JDK8 and JRE8 update 5 and it seems it works now. I guess I'll have to tell the people to download the JRE8 at least if they want to see the app.
PROBLEM 2: (SOLVED) I don't know how to include the images on my jar and make them work... I mean... They are in a folder called images into my project, and whenever I run the app from NetBeans it works smothly, but when I got it compiled as .jar (I know they are compiled correctly inside since the .jar size is 20MB+ and when I unzip it they are there.) they doesn't show on the app when running it (from cmd command line since as I said on problem 1, I can't run it doubleclicking since I get that mainclass error).
SOLUTION: As some people stated, the code I needed was:
java.net.URL imgUrl = getClass().getResource("/resources/images/Tuber melanosporum.jpg");
ImageIcon icon = new ImageIcon(imgUrl);
ImageIcon icono = new ImageIcon(icon.getImage().getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_DEFAULT));
jLabel2.setIcon(icono2);
The problem is noone told me that I needed to create a "resources" folder inside the "src" project folder and load it inside the project via Right Clicking Projects/Add/Other/Folder. What I had was an "images" folder in the main project folder. Please, try to specify a bit more since as I said, I'm super new to this whole scene. (Still got Problem 1)
EDIT: Thanks to all answers anyway, I don't know how to upvote your comments or how to mark them as an answer :/.