【问题标题】:Running a jar file that has an image运行具有图像的 jar 文件
【发布时间】:2015-07-11 13:17:35
【问题描述】:

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

  1. 我只能从 cmd 运行它,而不能通过双击它来运行 - 已从 Eclipse 将其导出为可运行的 jar 文件,并且我已经安装了 java 运行时环境。当我双击它时没有任何反应

  2. 我在eclipse中导入的图片没有导出到项目中

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

【问题讨论】:

标签: java eclipse jar


【解决方案1】:

为什么要从现有的 ImageIcon 创建一个新的 ImageIcon?亲!
此外,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");
        }
     }

    }

【讨论】:

  • 我得到了这个异常:Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source) at OpenImage.main(OpenImage.java:30)
  • 请检查您的图像文件是否确实存在,名称正确,位置正确。找不到文件时会出现此异常...这里的图像文件必须与源类位于同一包(文件夹)中。
  • 不客气。对于这个线程的未来读者来说,将您的代码恢复到其初始错误状态将是一件好事。
  • @Bob 感谢您恢复了初始代码。显示更改的常用方法是编辑初始帖子并在其末尾添加一个标记,如“编辑”,然后是新代码或任何你想要的。
猜你喜欢
  • 2012-05-18
  • 2012-02-16
  • 1970-01-01
  • 2023-03-25
  • 2016-12-24
  • 1970-01-01
  • 2014-03-26
  • 2016-05-05
  • 2011-06-22
相关资源
最近更新 更多