【问题标题】:Check if a jar directory contains a folder检查jar目录是否包含文件夹
【发布时间】:2014-05-10 21:44:48
【问题描述】:

嘿,我想检查一下我的 jar 从哪里运行(jar 的路径),该目录包含我正在尝试的某个文件夹:

 try{
    File fofjar=new File((PackLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath()));

    File f=new File(fofjar.toString()+"\\myfol");
    System.out.println(f.toPath());
    if (Files.exists(f.toPath())) {
       System.out.printlin("folder is there!")

    }
    else{
        throw new Exception("\n"+f.toPath());
    }

}catch(Exception e){
     JOptionPane.showMessageDialog(null, "directory does not exist\n" + e + "\nClass:Pack Loader", "Not Found Error", JOptionPane.ERROR_MESSAGE);
}

问题是返回的 jar 目录对我来说很好,但是当我的 jar 目录位置中存在目录 myfol 时出现一些问题,它仍然会引发异常,有人可以告诉我有什么问题我是这项工作的新手 谢谢

【问题讨论】:

  • 一个“jar目录”,即jar中的目录?
  • 包含我的 jar 的文件夹(检查包含我的 jar 的文件夹是否有 myfol)
  • 啊,好吧,这个不清楚。您应该直接使用Path,然后使用.resolveSibling()
  • 没有得到你的评论@fge
  • resolveSibling() 方法在哪里,哈哈

标签: java file jar path


【解决方案1】:

使用这个:

final Path dir = Paths.get(PackLoader.class.getProtectionDomain()
    .getCodeSource().getLocation().getPath());

return Files.isDirectory(dir.resolve("myfol"));

【讨论】:

  • 什么错误?你在你的问题中说抛出了一个异常,但你实际上并没有显示它
猜你喜欢
  • 2011-05-04
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2010-09-10
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多