Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取jar/war文件中的maven.properties文件_Java_Maven_Readfile - Fatal编程技术网

Java 读取jar/war文件中的maven.properties文件

Java 读取jar/war文件中的maven.properties文件,java,maven,readfile,Java,Maven,Readfile,有没有一种方法可以用Java读取jar/war文件中文件(maven.properties)的内容?我需要从磁盘读取文件,但它没有被使用(在内存中)。有没有关于如何做到这一点的建议 问候,, 约翰·基斯(Johan Kees)这绝对是可能的,尽管在不知道你的确切情况下,很难具体地说出来 String path = "META-INF/maven/pom.properties"; Properties prop = new Properties(); InputStream in = Class

有没有一种方法可以用Java读取jar/war文件中文件(
maven.properties
)的内容?我需要从磁盘读取文件,但它没有被使用(在内存中)。有没有关于如何做到这一点的建议

问候,,
约翰·基斯(Johan Kees)

这绝对是可能的,尽管在不知道你的确切情况下,很难具体地说出来

String path = "META-INF/maven/pom.properties";

Properties prop = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream(path );
try {
  prop.load(in);
} 
catch (Exception e) {

} finally {
    try { in.close(); } 
    catch (Exception ex){}
}
System.out.println("maven properties " + prop);
WAR和JAR文件基本上是.zip文件,因此如果您有包含所需.properties文件的文件的位置,您可以使用打开它并提取属性

但是,如果它是一个JAR文件,可能有一种更简单的方法:只需将其添加到类路径中,然后使用如下方式加载属性:

SomeClass.class.getClassLoader().getResourceAsStream("maven.properties"); 

(假设属性文件在根包中)

这是绝对可能的,尽管在不知道具体情况的情况下,很难说清楚

WAR和JAR文件基本上是.zip文件,因此如果您有包含所需.properties文件的文件的位置,您可以使用打开它并提取属性

但是,如果它是一个JAR文件,可能有一种更简单的方法:只需将其添加到类路径中,然后使用如下方式加载属性:

SomeClass.class.getClassLoader().getResourceAsStream("maven.properties"); 

(假设属性文件在根包中)

首先,从技术上讲,它不是一个文件。JAR/WAR是一个文件,您要查找的是归档文件(也称为资源)中的一个条目

因为它不是一个文件,所以需要将其作为
InputStream

  • 如果罐子/战争在 类路径,您可以执行
    SomeClass.class.getResourceAsStream(“/path/from/the/jar/to/maven.properties”)
    ,其中
    SomeClass
    是该jar/WAR中的任何类

    // these are equivalent:
    SomeClass.class.getResourceAsStream("/abc/def");
    SomeClass.class.getClassLoader().getResourceAsStream("abc/def");
    // note the missing slash in the second version
    
  • 如果没有,您将不得不像下面这样阅读JAR/WAR:

    JarFile jarFile = new JarFile(file);
    InputStream inputStream =
        jarFile.getInputStream(jarFile.getEntry("path/to/maven.properties"));
    

  • 现在,您可能希望将
    InputStream
    加载到
    Properties
    对象中:

    Properties props = new Properties();
    // or: Properties props = System.getProperties();
    props.load(inputStream);
    

    或者您可以将
    InputStream
    读入字符串。如果您使用像这样的库,这会容易得多


      • 首先,从技术上讲,它不是一个文件。JAR/WAR是一个文件,您要查找的是归档文件(也称为资源)中的一个条目

        因为它不是一个文件,所以需要将其作为
        InputStream

      • 如果罐子/战争在 类路径,您可以执行
        SomeClass.class.getResourceAsStream(“/path/from/the/jar/to/maven.properties”)
        ,其中
        SomeClass
        是该jar/WAR中的任何类

        // these are equivalent:
        SomeClass.class.getResourceAsStream("/abc/def");
        SomeClass.class.getClassLoader().getResourceAsStream("abc/def");
        // note the missing slash in the second version
        
      • 如果没有,您将不得不像下面这样阅读JAR/WAR:

        JarFile jarFile = new JarFile(file);
        InputStream inputStream =
            jarFile.getInputStream(jarFile.getEntry("path/to/maven.properties"));
        

      • 现在,您可能希望将
        InputStream
        加载到
        Properties
        对象中:

        Properties props = new Properties();
        // or: Properties props = System.getProperties();
        props.load(inputStream);
        

        或者您可以将
        InputStream
        读入字符串。如果您使用像这样的库,这会容易得多


        • ClassLoader.getsystemresourceastream()的+1不知道那一个
          ClassLoader.getsystemresourceastream()的+1不知道那一个