0

I'm trying to run a jar file but have got 2 problems:

  1. I can only run it from cmd and not by double clicking on it - Have exported it as a runnable jar file from Eclipse and I've installed java runtime enviroment. When I double click on it nothing happens

  2. The image I've imported in eclipse isn't exported the project

The jar file has to ask for a username/password and then open an image but it can't.

Code:

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class OpenImage {

public static void main(String[] args){
    String un;
    int pass;

    Image image = null;
    System.out.println("Welcome, please enter the username and password to open the image");
    Scanner scan = new Scanner(System.in);
    System.out.println("Username?:");
    un = scan.nextLine();
    System.out.println("Password?:");
    pass = scan.nextInt();

    if(un.equals("Hudhud") && pass==123){
        System.out.println("");
        System.out.println("Here you got the picture");
        try{
            File sourceimage = new File("index.jpg");
            image = ImageIO.read(sourceimage);
        }
        catch (IOException e){
            e.printStackTrace();
        }

        JFrame frame = new JFrame();
        frame.setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        frame.add(label);
        frame.setVisible(true);
    }
    else{
        System.out.println("You ain't the boss");
    }
 }

}

This is my project: http://www.filedropper.com/capture

Bob
  • 3
  • 1
  • 6
  • possible duplicate of [How do I read a resource file from a Java jar file?](http://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file) – SamTebbs33 Jul 11 '15 at 13:33
  • possible duplicate of [how to load the images in java runnable jar?](http://stackoverflow.com/questions/7963155/how-to-load-the-images-in-java-runnable-jar) – RealSkeptic Jul 11 '15 at 13:37
  • Hmm no, have also looked at http://stackoverflow.com/questions/25635636/eclipse-exported-runnable-jar-not-showing-images – Bob Jul 11 '15 at 13:37
  • Have tried to create a source folder and put the image in it and change the path so /Resource/index.jpg but I still have the same problem – Bob Jul 11 '15 at 13:43
  • But did you notice that you are **not** supposed to use `File` for reading it? – RealSkeptic Jul 11 '15 at 13:54
  • hmm, I should use URL instead? – Bob Jul 11 '15 at 14:06
  • Two things: the name of the file available from your link is Capture.PGN and not index.jpg. The second: when the class and the picture are under the same source folder, e.g. "src", then with "File sourceimage = new File("src/Capture.PGN");", everything runs fine under Eclipse. But it doesn't anymore from command line... – atao Jul 11 '15 at 14:14
  • My friend, the image is "index.jpg". But what should I do then? – Bob Jul 11 '15 at 14:18
  • Yes, URL indeed. May be something like "ImageIcon icon = new ImageIcon(OpenImage.class.getResource("index.jpg"));" where index.jpg (or whatever it is...) is under the same folder that the class file. This runs fine under Eclipse and from command line. – atao Jul 11 '15 at 14:31
  • Ok, what should I write after that `image = ImageIO.read(icon);` doesn't work – Bob Jul 11 '15 at 14:45
  • You don't need anymore the try/catch around "ImageIO.read(sourceimage);". Use directly the ImageIcon instance as argument of the JLabel ctor. – atao Jul 11 '15 at 14:55
  • Have edited the code, is this what you mean? – Bob Jul 11 '15 at 15:06
  • http://stackoverflow.com/a/25636097/2587435 – Paul Samsotha Jul 11 '15 at 15:11
  • Don't change your code. It's quite a bad idea. Doing so, the thread of comments becomes incomprehensible. See my answer for the code. – atao Jul 11 '15 at 15:27

1 Answers1

0

Why do you want to create a new ImageIcon from an existing one? KISS!
Furthermore no try/catch is required by Class.getResource.

So:



    import java.net.URL;
    import java.util.Scanner;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    public class OpenImage {

        private static final String IMG_FILE_PATH = "index.jpg";
        private static final String USERNAME = "Hudhud";
        private static final int PASSWORD = 123;

    public static void main(String[] args){
        String un;
        int pass;

        System.out.println("Welcome, please enter the username and password to open the image");
        Scanner scan = new Scanner(System.in);
        System.out.println("Username?:");
        un = scan.nextLine();
        System.out.println("Password?:");
        pass = scan.nextInt();

        if(un.equals(USERNAME) && pass==PASSWORD){
            System.out.println();
            System.out.println("Here you got the picture");
            URL url = OpenImage.class.getResource(IMG_FILE_PATH);
            ImageIcon icon = new ImageIcon(url);
            JFrame frame = new JFrame();
            frame.setSize(300, 300);
            JLabel label = new JLabel(icon);
            frame.add(label);
            frame.setVisible(true);
        }
        else{
            System.out.println("You ain't the boss");
        }
     }

    }

atao
  • 835
  • 6
  • 13
  • I get this exception: `Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) at OpenImage.main(OpenImage.java:30) ` – Bob Jul 11 '15 at 15:42
  • Please, check that your image file does really exist, with the right name and at the right location. You get this exception when the file is not found... Here the image file must be in the same package (folder) as the source class. – atao Jul 11 '15 at 15:54
  • You're welcome. It'll be kind for the futur readers of this thread to restore your code in its initial wrong state. – atao Jul 11 '15 at 16:04
  • @Bob Thanks for having restored the initial code. The usual way to display changes is to edit the initial post and add at the end of it a mark like "EDIT" and then the new code or whatever you want. – atao Jul 12 '15 at 11:51