在开发阶段一直使用以下方式调试没有问题:


String path = KStream104.class.getResource("/").getFile().toString();
File database = new File(path + Constants.geoIPFile);

但是,打包编译后,放到linux环境下,发现报空指针错误,这里的getResource("/")不再起作用了,并且返回值为null。
后面采用了下面的方式:

	static {
		String path = null;
		try {
			URL url = KStream104.class.getResource("/");
                        
            //获取当前用户的绝对路径
			String userdir = System.getProperty("user.dir");
			if(url!=null){  // 先判断url
				path = url.getFile();
			} 
			else if(StringUtils.isNotBlank(userdir)){  //再判断userdir
				path = userdir.endsWith("/") ? userdir : userdir + "/";
			} else {   //否则为空
				path = "";
			}
			System.out.println("path:" + path);
			log.info(path + Constants.geoIPFile);
			File database = new File(path + Constants.geoIPFile);
			reader = new DatabaseReader.Builder(database).build();
			
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}


相关文章:

  • 2022-12-23
  • 2021-06-12
  • 2021-11-20
  • 2022-01-31
  • 2021-11-27
  • 2022-12-23
  • 2022-02-07
猜你喜欢
  • 2022-12-23
  • 2021-10-06
  • 2021-07-29
  • 2022-01-29
  • 2021-12-20
  • 2022-12-23
  • 2021-10-12
相关资源
相似解决方案