运行具有图像的jar文件

时间:2015-07-11 13:17:36

标签: java eclipse jar

我正在尝试运行一个jar文件,但遇到了两个问题:

  1. 我只能从cmd运行它而不是双击它 - 从Eclipse导出它作为一个可运行的jar文件,我已经安装了java运行环境。当我双击它时没有任何反应

  2. 我在eclipse中导入的图像未导出项目

  3. jar文件必须要求输入用户名/密码,然后打开图像,但不能。

    代码:

    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");
        }
     }
    
    }
    

    这是我的项目: http://www.filedropper.com/capture

1 个答案:

答案 0 :(得分:0)

为什么要从现有的ImageIcon创建新的ImageIcon? KISS!
此外,Class.getResource不需要try / catch。

所以:



    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");
        }
     }

    }