1

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 :/.

Jf Beaulac
  • 5,206
  • 1
  • 25
  • 46
eXtasis
  • 11
  • 3
  • in order to run a .jar file, you need to have a correct manifest.mf file pointing to the correct entry point, and, in order to get it done by 'clicking', your os needs to have it's environment settings (jre) correct. if it doesn't, your os doesn't know how to handle a .jar – Stultuske May 29 '14 at 09:19
  • Where does the manifest has to be? I got it on the project folder and it's code is edited on main post. Since I have JDK and JRE installed I thought I didn't need anything else, where should I look for to change that setting? Thank you – eXtasis May 29 '14 at 09:30
  • check the jar file itself, unzip it and check the manifest file. just make sure it is pointing to the correct main method – Stultuske May 29 '14 at 09:32
  • Whenever I compile a project, I get a ".jar" and a ".txt" (README) file on my dist folder, there is not ".zip" or anything :/. And I don't know how exactly manifests works. As I said, what I have is what I edited on the main post. Sorry for all the inconveniences. – eXtasis May 29 '14 at 09:34
  • a .jar file is a .zip file, with it's own extension. unzip it, and check the manifest.mf file that's in there – Stultuske May 29 '14 at 09:35
  • Wow you are right, thank you. Check the Problem 1 in the main post for a little update. I still don't know what to do tho... – eXtasis May 29 '14 at 09:39
  • *My app, hasn't got a main class, It's only a jForm*: a main class can be any class that has a main method specified (program entry point). If you are able to run/debug inside IDE, then you already have one specified. If you used Netbeans GUI builder to create your JFrame, it is automatically added. – predi May 29 '14 at 12:25
  • See [this answer](http://stackoverflow.com/a/13012205/878469) to find out how to make your images work. You need to address them via paths that are relative to a class. – predi May 29 '14 at 12:34
  • Yeah, I just noticed there is a public main class etc. at the end of the jFrameform class I'm running with a try catch automatically written in it and something else. So, the question is still here, why does the system says cannot find main class? Is manifest incorrect? And about the images, I've seen those posts but can't understand them at all.. :/ – eXtasis May 29 '14 at 18:44
  • Why are you not exporting as an executable jar file? Netbeans does that. – Mc Kevin May 30 '14 at 08:14
  • Well, I think that's what I'm doing. I'm Right Clicking my Project and selecting "Clean and Build". Then I got a .jar in my dist folder but whenever I try to execute it, it says that it cannot find the main class :/ – eXtasis May 30 '14 at 08:49

0 Answers0