包含带有Jar文件的属性文件

时间:2023-01-13 12:59:59

I've written a small application. I've put the database specific information in a properties file.

我写了一个小应用程序。我将数据库特定的信息放在属性文件中。

db.url=jdbc:mysql://localhost:3306/librarydb
db.user=root
db.passwd=pas$w0rd

When I build the application to get the executable jar file, the properties file gets included inside. The whole purpose of putting those information in a properties file is to allow the user to change those but this is making it impossible.

当我构建应用程序以获取可执行jar文件时,属性文件将包含在其中。将这些信息放入属性文件的全部目的是允许用户更改这些信息,但这样做是不可能的。

How can I include a properties file with the jar file not in it? Kinda like you do with AppSettings file in .NET applications.

如何将一个带有jar文件的属性文件包含进来?有点像在。net应用程序中使用AppSettings文件。

I'm very new to Java so I'd appreciate if you could break it down to a set of steps.

我对Java很陌生,所以我希望你能把它分解成一组步骤。

Thank you.

谢谢你!

EDIT :

编辑:

This is how I'm getting the values from the properties file in my program.

这就是如何从程序中的属性文件中获取值的方法。

public class DatabaseHandler {

    public static Connection connectToDb() {
        Connection con = null;
        Properties props = new Properties();
        InputStream in = null;

        try {
            in = DatabaseHandler.class.getResourceAsStream("database.properties");
            props.load(in);
            in.close();

            String url = props.getProperty("db.url");
            String user = props.getProperty("db.user");
            String password = props.getProperty("db.passwd");

            con = DriverManager.getConnection(url, user, password);
        } catch (Exception ex) {        
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex); 
        }
            return con;
    }

}

4 个解决方案

#1


6  

You are right. If the properties are intended to be modifiable by the user, then putting them in a properties file in the JAR is not going to work

你是对的。如果属性打算由用户修改,那么将它们放在JAR中的属性文件中将不起作用

There are a couple of approaches:

有几个方法:

  • You could put the properties in a property file that you store in the file system. The problem is figuring out where in the filesystem to store them:

    您可以将属性放在存储在文件系统中的属性文件中。问题是要弄清楚在文件系统中存储它们的位置:

    • On UNIX / Linux there are conventions for where to store system-wide and per-user configuration properties, but these are a bit loose, and in a bit of a state of flux (e.g. certain "desktop" frameworks have their own preference mechanisms.

      在UNIX / Linux上,对于存储系统范围和每个用户的配置属性,有一些约定,但是这些约定有点松散,并且处于一种不断变化的状态(例如,某些“桌面”框架有它们自己的首选机制。

    • There are similar conventions for Windows I believe.

      我认为Windows也有类似的惯例。

    • The problem with approach can be permissions issues (you don't want one user changing another's preferences), and possibly even issues with read-only file systems and the like.

      方法的问题可能是权限问题(您不希望一个用户更改另一个用户的首选项),甚至可能是只读文件系统之类的问题。

    • If you are going to use a property file in the filesystem, it is a bad idea to hardwire the location into your code. Make it configurable via a command line argument and/or a "system property" that can be set using the -D option.

      如果您打算在文件系统中使用属性文件,那么最好将位置硬连接到代码中。通过命令行参数和/或可以使用-D选项设置的“系统属性”来配置它。

  • On Windows, you could put the properties into the Windows registry. But you will need to use 3rd party libraries to do this. And of course, this is not portable - it won't work on a non-Windows platform.

    在Windows上,可以将属性放到Windows注册表中。但是您需要使用第三方库来实现这一点。当然,这并不是可移植的——它不能在非windows平台上运行。

  • You could use the Java Preferences API. This is portable across multiple platforms but it is a Java-centric solution; i.e. it doesn't fit well with the platform's "normal way" of doing things.

    您可以使用Java Preferences API。这是可以跨多个平台移植的,但它是一个以java为中心的解决方案;也就是说,它不太符合平台的“正常方式”。

In short, there is no solution that is ideal from all perspectives.

简而言之,从各个角度来看,没有理想的解决方案。


But once you've decided how to store your preferences, you could write your application so that it uses the properties file in your JAR file to provide default or initial settings.

但是,一旦您决定了如何存储首选项,您就可以编写应用程序,以便它使用JAR文件中的属性文件来提供默认或初始设置。

#2


2  

You can load a property file from the classpath instead. This enables you to bundle the property file inside the jar, but also outside the jar. It just needs to be on the classpath of the running application.

您可以从类路径加载一个属性文件。这使您可以将属性文件打包到jar中,也可以打包到jar之外。它只需要位于正在运行的应用程序的类路径上。

You can do so in the following way:

你可以这样做:

Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader()
        .getResourceAsStream(propFileName);

props.load(inputStream);

#3


2  

instead of

而不是

in = DatabaseHandler.class.getResourceAsStream("database.properties");

try

试一试

in = DatabaseHandler.class.getResourceAsStream("/database.properties");

#4


0  

Do not hardcode the location of property file. Your approach should be to stream the file from classpath.

不要硬编码属性文件的位置。您的方法应该是从类路径中传输文件。

If not, the better way would be to point to a URL which returns the properties information in a specific data format (say JSON).

如果不是,更好的方法是指向一个URL,该URL以特定的数据格式返回属性信息(比如JSON)。

If you have JAR check this out.

如果你有罐子,看看这个。

If your application is WAR check properties in WAR.

如果您的应用程序是WAR check属性。

#1


6  

You are right. If the properties are intended to be modifiable by the user, then putting them in a properties file in the JAR is not going to work

你是对的。如果属性打算由用户修改,那么将它们放在JAR中的属性文件中将不起作用

There are a couple of approaches:

有几个方法:

  • You could put the properties in a property file that you store in the file system. The problem is figuring out where in the filesystem to store them:

    您可以将属性放在存储在文件系统中的属性文件中。问题是要弄清楚在文件系统中存储它们的位置:

    • On UNIX / Linux there are conventions for where to store system-wide and per-user configuration properties, but these are a bit loose, and in a bit of a state of flux (e.g. certain "desktop" frameworks have their own preference mechanisms.

      在UNIX / Linux上,对于存储系统范围和每个用户的配置属性,有一些约定,但是这些约定有点松散,并且处于一种不断变化的状态(例如,某些“桌面”框架有它们自己的首选机制。

    • There are similar conventions for Windows I believe.

      我认为Windows也有类似的惯例。

    • The problem with approach can be permissions issues (you don't want one user changing another's preferences), and possibly even issues with read-only file systems and the like.

      方法的问题可能是权限问题(您不希望一个用户更改另一个用户的首选项),甚至可能是只读文件系统之类的问题。

    • If you are going to use a property file in the filesystem, it is a bad idea to hardwire the location into your code. Make it configurable via a command line argument and/or a "system property" that can be set using the -D option.

      如果您打算在文件系统中使用属性文件,那么最好将位置硬连接到代码中。通过命令行参数和/或可以使用-D选项设置的“系统属性”来配置它。

  • On Windows, you could put the properties into the Windows registry. But you will need to use 3rd party libraries to do this. And of course, this is not portable - it won't work on a non-Windows platform.

    在Windows上,可以将属性放到Windows注册表中。但是您需要使用第三方库来实现这一点。当然,这并不是可移植的——它不能在非windows平台上运行。

  • You could use the Java Preferences API. This is portable across multiple platforms but it is a Java-centric solution; i.e. it doesn't fit well with the platform's "normal way" of doing things.

    您可以使用Java Preferences API。这是可以跨多个平台移植的,但它是一个以java为中心的解决方案;也就是说,它不太符合平台的“正常方式”。

In short, there is no solution that is ideal from all perspectives.

简而言之,从各个角度来看,没有理想的解决方案。


But once you've decided how to store your preferences, you could write your application so that it uses the properties file in your JAR file to provide default or initial settings.

但是,一旦您决定了如何存储首选项,您就可以编写应用程序,以便它使用JAR文件中的属性文件来提供默认或初始设置。

#2


2  

You can load a property file from the classpath instead. This enables you to bundle the property file inside the jar, but also outside the jar. It just needs to be on the classpath of the running application.

您可以从类路径加载一个属性文件。这使您可以将属性文件打包到jar中,也可以打包到jar之外。它只需要位于正在运行的应用程序的类路径上。

You can do so in the following way:

你可以这样做:

Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader()
        .getResourceAsStream(propFileName);

props.load(inputStream);

#3


2  

instead of

而不是

in = DatabaseHandler.class.getResourceAsStream("database.properties");

try

试一试

in = DatabaseHandler.class.getResourceAsStream("/database.properties");

#4


0  

Do not hardcode the location of property file. Your approach should be to stream the file from classpath.

不要硬编码属性文件的位置。您的方法应该是从类路径中传输文件。

If not, the better way would be to point to a URL which returns the properties information in a specific data format (say JSON).

如果不是,更好的方法是指向一个URL,该URL以特定的数据格式返回属性信息(比如JSON)。

If you have JAR check this out.

如果你有罐子,看看这个。

If your application is WAR check properties in WAR.

如果您的应用程序是WAR check属性。