1

I'm using Netbeans 8.1, I follow this instruction Packaging and Distributing Java Desktop Applications to build executable file(.jar), but when I double-click to run application, it show error window with a message"Could not find the main class".
Here is configuration of Netbeans use to build .jar file:
enter image description here

enter image description here

enter image description here

Here is contain of manifest file inside .jar file enter image description here

Inside the .jar file, it also include class in which main class is declared. What else do I need to do to build executable file?

[Edit]

java -jar myfile.jar 

enter image description here

jar -tvf myfile.jar

Images directory not show here, but can see by 7zip enter image description here

dir lib

enter image description here

Here is diplayImage() function

  private  String IMG_PATH = "src/images/";
  public  void displayImage(boolean  isOK){
    String imgOkFileName = "gateOK.png";
    String imgNGFileName = "gateNG.png";
    BufferedImage img = null;
    if(isOK){          
        try{
              img = ImageIO.read(new File(IMG_PATH+imgOkFileName));
        }catch(IOException e){
            Logger.getLogger(PTCMainFrame.class.getName()).log(Level.SEVERE, null, e);
        }
       Image dimg =  img.getScaledInstance(lbImage.getWidth(), lbImage.getHeight(),
                Image.SCALE_SMOOTH);

        ImageIcon image = new ImageIcon(dimg);
        if(image == null) lbImage.setText("Image not found");
        else{
            lbImage.setIcon(image);
        }
    }else{
        try{
              img = ImageIO.read(new File(IMG_PATH+imgNGFileName));
        }catch(IOException e){
              Logger.getLogger(PTCMainFrame.class.getName()).log(Level.SEVERE, null, e);
        }
       Image dimg =  img.getScaledInstance(lbImage.getWidth(), lbImage.getHeight(),
                Image.SCALE_SMOOTH);

        ImageIcon image = new ImageIcon(dimg);
        if(image == null) lbImage.setText("Image not found");
        else{
            lbImage.setIcon(image);

        }
    }
}
neo
  • 618
  • 1
  • 10
  • 29
  • 1- Try a clean and build; 2- Check the contents of the build dir to make sure that the jar file and the lib directory (and it's contents) are correct; 3- Make sure you copy the jar file AND lib directory together if you move the jar to another location – MadProgrammer Dec 29 '15 at 06:51
  • 1- I tried clean and build 2- lib directory contain jctasks.jar and AbsoluteLayout.jar as specify in above manifest file, executable file contain all packages and classes of project. 3- I run the executable file on project directory which is created by Netbeans. Do you have any suggestion for me ? Thanks. – neo Dec 29 '15 at 07:03
  • Open a command line, go to the directory where your jar file is located, and execute `java -jar yourjar.jar`. Paste the entire output in your question. Then execute `jar tvf yourjar.jar`. Paste the output in your question. Then execute `ls lib` or `dir lib`on Windows. Paste the output in your question. – JB Nizet Dec 29 '15 at 07:06
  • I think the reason is the executable file doesn't contain images directory which contain some images, so the display image function does not work. I created images directory in my project and store some images in it. How can I configure NetBeans to build executable file which include images directory? – neo Dec 29 '15 at 07:43
  • Your actually problem seems to be an issue with reading your image, which is causing a `NullPointerException`, what does he line 559 in `PTCMainFrame` (`displayImage`) actually look like – MadProgrammer Dec 29 '15 at 08:11
  • I've edit my question, could you look at it. I moved images directory into src directory and re-build, now executable file contain images directory, but when I run application, displayImage() function still throw NullPointerException – neo Dec 29 '15 at 08:26
  • 4
    Your code assumes that images are in a subdirectory directory src/images of the current directory. It shouldn't make such an assumption. Instead, the images should be inside the jar file, and should be loaded by the class loader, rather than with file IO. Read http://stackoverflow.com/questions/31127/java-swing-displaying-images-from-within-a-jar. – JB Nizet Dec 29 '15 at 08:40

0 Answers0