用Jar文件包含属性文件 [英] Include A Properties File With A Jar File

查看:175
本文介绍了用Jar文件包含属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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



如何包含属性文件jar文件不在里面? Kinda就像你在.NET应用程序中使用AppSettings文件一样。



我对Java非常新,所以如果你可以将其分解为一系列步骤。



谢谢。



编辑



这是我从属性文件。

  public class DatabaseHandler {

public static Connection connectToDb b $ b Connection con = null;
属性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;
}

}


解决方案

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



有几种方法: / p>


  • 您可以将属性放在存储在文件系统中的属性文件中。




    • 在UNIX / Linux上,是在哪里存储系统范围和每个用户配置属性的约定,但是这些有点松散,并且处于一定程度的通量状态(例如,某些桌面框架具有它们自己的偏好机制。)

      / li>
    • 对于Windows我有相似的约定。


    • 方法的问题可以是权限问题


    • 如果您要使用一个只读文件系统,属性文件在文件系统中,将一个位置硬连接到你的代码是一个坏主意使它可以通过命令行参数和/或系统属性可以通过使用 -D


  • 在Windows上,您可以将属性放入Windows注册表。但是你需要使用第三方库来做到这一点。当然,这不是可移植的 - 它不能在非Windows平台上工作。


  • Java首选项API 。这是可移植跨多个平台,但它是一个以Java为中心的解决方案;即




简而言之,






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


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.

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

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

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;
    }

}

解决方案

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

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.

    • There are similar conventions for Windows I believe.

    • 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.

  • 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.

  • 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.

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文件包含属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆