读取JAR文件外的属性文件

时间:2023-02-05 14:01:50

I have a JAR file where all my code is archived for running. I have to access a properties file which need to be changed/edited before each run. I want to keep the properties file in the same directory where the JAR file is. Is there anyway to tell Java to pick up the properties file from that directory ?

我有一个JAR文件,我的所有代码都存档以便运行。我必须访问每次运行前需要更改/编辑的属性文件。我想将属性文件保存在JAR文件所在的目录中。有没有告诉Java从该目录中获取属性文件?

Note: I do not want to keep the properties file in home directory or pass the path of the properties file in command line argument.

注意:我不想将属性文件保留在主目录中,也不希望在命令行参数中传递属性文件的路径。

4 个解决方案

#1


116  

So, you want to treat your .properties file on the same folder as the main/runnable jar as a file rather than as a resource of the main/runnable jar. In that case, my own solution is as follows:

因此,您希望将.properties文件作为main / runnable jar放在与main / runnable jar相同的文件夹中,而不是作为main / runnable jar的资源。在这种情况下,我自己的解决方案如下:

First thing first: your program file architecture shall be like this (assuming your main program is main.jar and its main properties file is main.properties):

首先要做的是:你的程序文件架构应该是这样的(假设你的主程序是main.jar,它的主要属性文件是main.properties):

./ - the root of your program
 |__ main.jar
 |__ main.properties

With this architecture, you can modify any property in the main.properties file using any text editor before or while your main.jar is running (depending on the current state of the program) since it is just a text-based file. For example, your main.properties file may contain:

使用此体系结构,您可以在main.jar运行之前或期间使用任何文本编辑器修改main.properties文件中的任何属性(取决于程序的当前状态),因为它只是一个基于文本的文件。例如,您的main.properties文件可能包含:

app.version=1.0.0.0
app.name=Hello

So, when you run your main program from its root/base folder, normally you will run it like this:

因此,当您从其root / base文件夹运行主程序时,通常您将按以下方式运行它:

java -jar ./main.jar

or, straight away:

或者,马上:

java -jar main.jar

In your main.jar, you need to create a few utility methods for every property found in your main.properties file; let say the app.version property will have getAppVersion() method as follows:

在main.jar中,您需要为main.properties文件中的每个属性创建一些实用程序方法;假设app.version属性将具有getAppVersion()方法,如下所示:

/**
 * Gets the app.version property value from
 * the ./main.properties file of the base folder
 *
 * @return app.version string
 * @throws IOException
 */
public static String getAppVersion() throws IOException{

    String versionString = null;

    //to load application's properties, we use this class
    Properties mainProperties = new Properties();

    FileInputStream file;

    //the base folder is ./, the root of the main.properties file  
    String path = "./main.properties";

    //load the file handle for main.properties
    file = new FileInputStream(path);

    //load all the properties from this file
    mainProperties.load(file);

    //we have loaded the properties, so close the file handle
    file.close();

    //retrieve the property we are intrested, the app.version
    versionString = mainProperties.getProperty("app.version");

    return versionString;
}

In any part of the main program that needs the app.version value, we call its method as follows:

在需要app.version值的主程序的任何部分中,我们将其方法调用如下:

String version = null;
try{
     version = getAppVersion();
}
catch (IOException ioe){
    ioe.printStackTrace();
}

#2


29  

I did it by other way.

我通过其他方式做到了。

Properties prop = new Properties();
    try {

        File jarPath=new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        String propertiesPath=jarPath.getParentFile().getAbsolutePath();
        System.out.println(" propertiesPath-"+propertiesPath);
        prop.load(new FileInputStream(propertiesPath+"/importer.properties"));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
  1. Get Jar file path.
  2. 获取Jar文件路径。
  3. Get Parent folder of that file.
  4. 获取该文件的Parent文件夹。
  5. Use that path in InputStreamPath with your properties file name.
  6. 在InputStreamPath中使用该路径和您的属性文件名。

#3


1  

I have a similar case: wanting my *.jar file to access a file in a directory next to said *.jar file. Refer to THIS ANSWER as well.

我有一个类似的情况:想要我的* .jar文件访问所述* .jar文件旁边的目录中的文件。请参阅此答案。

My file structure is:

我的文件结构是:

./ - the root of your program
|__ *.jar
|__ dir-next-to-jar/some.txt

I'm able to load a file (say, some.txt) to an InputStream inside the *.jar file with the following:

我可以使用以下内容将文件(例如some.txt)加载到* .jar文件中的InputStream:

InputStream stream = null;
    try{
        stream = ThisClassName.class.getClass().getResourceAsStream("/dir-next-to-jar/some.txt");
    }
    catch(Exception e) {
        System.out.print("error file to stream: ");
        System.out.println(e.getMessage());
    }

Then do whatever you will with the stream

然后用流做任何你想做的事

#4


0  

There's always a problem accessing files on your file directory from a jar file. Providing the classpath in a jar file is very limited. Instead try using a bat file or a sh file to start your program. In that way you can specify your classpath anyway you like, referencing any folder anywhere on the system.

从jar文件访问文件目录中的文件始终存在问题。在jar文件中提供类路径非常有限。而是尝试使用bat文件或sh文件来启动程序。通过这种方式,您可以随意指定类路径,引用系统中任何位置的任何文件夹。

Also check my answer on this question:

另请查看我对这个问题的回答:

making .exe file for java project containing sqlite

为包含sqlite的java项目制作.exe文件

#1


116  

So, you want to treat your .properties file on the same folder as the main/runnable jar as a file rather than as a resource of the main/runnable jar. In that case, my own solution is as follows:

因此,您希望将.properties文件作为main / runnable jar放在与main / runnable jar相同的文件夹中,而不是作为main / runnable jar的资源。在这种情况下,我自己的解决方案如下:

First thing first: your program file architecture shall be like this (assuming your main program is main.jar and its main properties file is main.properties):

首先要做的是:你的程序文件架构应该是这样的(假设你的主程序是main.jar,它的主要属性文件是main.properties):

./ - the root of your program
 |__ main.jar
 |__ main.properties

With this architecture, you can modify any property in the main.properties file using any text editor before or while your main.jar is running (depending on the current state of the program) since it is just a text-based file. For example, your main.properties file may contain:

使用此体系结构,您可以在main.jar运行之前或期间使用任何文本编辑器修改main.properties文件中的任何属性(取决于程序的当前状态),因为它只是一个基于文本的文件。例如,您的main.properties文件可能包含:

app.version=1.0.0.0
app.name=Hello

So, when you run your main program from its root/base folder, normally you will run it like this:

因此,当您从其root / base文件夹运行主程序时,通常您将按以下方式运行它:

java -jar ./main.jar

or, straight away:

或者,马上:

java -jar main.jar

In your main.jar, you need to create a few utility methods for every property found in your main.properties file; let say the app.version property will have getAppVersion() method as follows:

在main.jar中,您需要为main.properties文件中的每个属性创建一些实用程序方法;假设app.version属性将具有getAppVersion()方法,如下所示:

/**
 * Gets the app.version property value from
 * the ./main.properties file of the base folder
 *
 * @return app.version string
 * @throws IOException
 */
public static String getAppVersion() throws IOException{

    String versionString = null;

    //to load application's properties, we use this class
    Properties mainProperties = new Properties();

    FileInputStream file;

    //the base folder is ./, the root of the main.properties file  
    String path = "./main.properties";

    //load the file handle for main.properties
    file = new FileInputStream(path);

    //load all the properties from this file
    mainProperties.load(file);

    //we have loaded the properties, so close the file handle
    file.close();

    //retrieve the property we are intrested, the app.version
    versionString = mainProperties.getProperty("app.version");

    return versionString;
}

In any part of the main program that needs the app.version value, we call its method as follows:

在需要app.version值的主程序的任何部分中,我们将其方法调用如下:

String version = null;
try{
     version = getAppVersion();
}
catch (IOException ioe){
    ioe.printStackTrace();
}

#2


29  

I did it by other way.

我通过其他方式做到了。

Properties prop = new Properties();
    try {

        File jarPath=new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        String propertiesPath=jarPath.getParentFile().getAbsolutePath();
        System.out.println(" propertiesPath-"+propertiesPath);
        prop.load(new FileInputStream(propertiesPath+"/importer.properties"));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
  1. Get Jar file path.
  2. 获取Jar文件路径。
  3. Get Parent folder of that file.
  4. 获取该文件的Parent文件夹。
  5. Use that path in InputStreamPath with your properties file name.
  6. 在InputStreamPath中使用该路径和您的属性文件名。

#3


1  

I have a similar case: wanting my *.jar file to access a file in a directory next to said *.jar file. Refer to THIS ANSWER as well.

我有一个类似的情况:想要我的* .jar文件访问所述* .jar文件旁边的目录中的文件。请参阅此答案。

My file structure is:

我的文件结构是:

./ - the root of your program
|__ *.jar
|__ dir-next-to-jar/some.txt

I'm able to load a file (say, some.txt) to an InputStream inside the *.jar file with the following:

我可以使用以下内容将文件(例如some.txt)加载到* .jar文件中的InputStream:

InputStream stream = null;
    try{
        stream = ThisClassName.class.getClass().getResourceAsStream("/dir-next-to-jar/some.txt");
    }
    catch(Exception e) {
        System.out.print("error file to stream: ");
        System.out.println(e.getMessage());
    }

Then do whatever you will with the stream

然后用流做任何你想做的事

#4


0  

There's always a problem accessing files on your file directory from a jar file. Providing the classpath in a jar file is very limited. Instead try using a bat file or a sh file to start your program. In that way you can specify your classpath anyway you like, referencing any folder anywhere on the system.

从jar文件访问文件目录中的文件始终存在问题。在jar文件中提供类路径非常有限。而是尝试使用bat文件或sh文件来启动程序。通过这种方式,您可以随意指定类路径,引用系统中任何位置的任何文件夹。

Also check my answer on this question:

另请查看我对这个问题的回答:

making .exe file for java project containing sqlite

为包含sqlite的java项目制作.exe文件