swing 项目,打包、找不到图片、打成EXE找不到环境的问题

1. 创建项目

1.1 普通java项目

可以直接创建java项目,然后把各种依赖包都贴进来。

 

项目结构:

在src里面写swing代码即可。有依赖资源直接贴进来(dll、so、图片等),不用创建resource文件夹。

1.2 maven项目

 

 资源文件可以放在resources里

依赖jar可以通过pom导入,不需要外部导入了。 

1.3 导出jar

右键项目,Export

 

 Finish

2.程序读取图片路径代码

URL logoUrl = this.getClass().getResource("/xxx.jpg"); // 打包后class的根目录有这个图
Image image = new ImageIcon(logoUrl).getImage();
this.setIconImage(image); // this 指当前JFrame

 3.关于配置文件外提

和读取图片文件一样,可以写properties文件,然后通过java工具类读取,在程序中使用。或者按行读取文本。

/**
	 * 按行读取exe同目录下的camera.ini
	 * @return
	 */
	public static List<String> readIni() {
		List<String> conf = new ArrayList<String>();
		String confPath = System.getProperty("user.dir") + "/camera.ini";
		System.out.println(confPath);
    	//读取配置文件
    	try {
			InputStream in = new BufferedInputStream (new FileInputStream(confPath));
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
			
			String str = null;
			while((str = bufferedReader.readLine()) != null)
			{
				conf.add(str);
				System.out.println(str);
			}
			in.close();
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return conf;
	}

 

4.打exe流程

 

 

 

 

 

 

 一路next

 5.部署流程

 把依赖的jre和库文件都放到C盘根目录即可(因为打exe时已经指定了path)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值