代码之家  ›  专栏  ›  技术社区  ›  Lehane

从JAR目录读取属性文件

  •  5
  • Lehane  · 技术社区  · 15 年前

    我正在创建一个可执行JAR,它将在运行时从一个文件中读取一组属性。目录结构如下:

    /some/dirs/executable.jar
    /some/dirs/executable.properties
    

    有没有一种方法可以在executable.jar文件中设置属性加载器类,从jar所在的目录中加载属性,而不是对目录进行硬编码。

    我不想把属性放在jar本身中,因为属性文件需要可配置。

    2 回复  |  直到 10 年前
        1
  •  13
  •   Adamski    15 年前

    为什么不把属性文件作为参数传递给主方法呢?这样就可以按如下方式加载属性:

    public static void main(String[] args) throws IOException {
      Properties props = new Properties();
      props.load(new BufferedReader(new FileReader(args[0])));
      System.setProperties(props);
    }
    

    另一种方法是:如果你想得到JAR文件的当前目录,你需要做一些令人讨厌的事情,比如:

    CodeSource codeSource = MyClass.class.getProtectionDomain().getCodeSource();
    File jarFile = new File(codeSource.getLocation().toURI().getPath());
    File jarDir = jarFile.getParentFile();
    
    if (jarDir != null && jarDir.isDirectory()) {
      File propFile = new File(jarDir, "myFile.properties");
    }
    

    …哪里 MyClass 是JAR文件中的类。不过,这不是我推荐的——如果你的应用有多个 类名 不同JAR文件中类路径上的实例(每个JAR在不同的目录中)?也就是说,你永远不能保证 类名 是从你认为的罐子里装出来的。

        2
  •  0
  •   amsathishkumar    10 年前

    public static void loadJarCongFile(Class Utilclass )
        {
           try{         
                 String path= Utilclass.getResource("").getPath();
                 path=path.substring(6,path.length()-1);
                 path=path.split("!")[0];
                 System.out.println(path);
                 JarFile jarFile = new JarFile(path);
    
                   final Enumeration<JarEntry> entries = jarFile.entries();
                   while (entries.hasMoreElements()) {
                       final JarEntry entry = entries.nextElement();
                       if (entry.getName().contains(".properties")) {
                           System.out.println("Jar File Property File: " + entry.getName());
                           JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                           InputStream input = jarFile.getInputStream(fileEntry);
                           setSystemvariable(input);      
                           InputStreamReader isr = new InputStreamReader(input); 
                           BufferedReader reader = new BufferedReader(isr);
                           String line;
                        
                           while ((line = reader.readLine()) != null) {
                               System.out.println("Jar file"+line);
                           }
                           reader.close();
                       }
                   }
           }
           catch (Exception e)
           {
              System.out.println("Jar file reading Error");
           }
        }
        public static void setSystemvariable(InputStream input)
        {
        Properties tmp1 = new Properties();
           try {
                 tmp1.load(input);
    
           for (Object element : tmp1.keySet()) {
                 System.setProperty(element.toString().trim(),
                               tmp1.getProperty(element.toString().trim()).trim());
                 }      
           } catch (IOException e) {
                 System.out.println("setSystemvariable method failure");
           }
        }