简体   繁体   English

使用Java控制台程序中的.properties文件

[英]Using .properties file from Java console program

I have created a Java program to compare scripts saved as files in the version management tool to those loaded in our database. 我创建了一个Java程序,将版本管理工具中保存为文件的脚本与数据库中加载的脚本进行比较。 It's a simple program, runs through start to finish and outputs to the console when it finds a discrepancy. 这是一个简单的程序,从头到尾运行,并在发现差异时输出到控制台。 Now I want to load the database URL, username and password as well as the location of my files from a .properties file. 现在我想从.properties文件加载数据库URL,用户名和密码以及我的文件位置。

I did assume that if I put the file on the classpath it would be visible from my Java program: 我确实假设如果我将文件放在类路径上,它将从我的Java程序中可见:

Properties values = new Properties();
try
{
    File checkPackages = new File("myfile.properties");
    if(!checkPackages.exists()) throw new FileNotFoundException();

    values.load(new FileReader(checkPackages));
}
catch(FileNotFoundException fnfe) {}

I also wanted to save this whole program to a .jar file so that it would be that bit more usable. 我还想将整个程序保存到.jar文件中,以便更有用。 Unfortunately, the only way I have found to reference the .properties file is to have it in the directory where I am running java.exe. 不幸的是,我发现引用.properties文件的唯一方法是将它放在我运行java.exe的目录中。 The PATH or the CLASSPATH don't seem to apply?? PATH或CLASSPATH似乎不适用??

I found an Oracle site about the .jar file's Manifest file as I was hoping there'd be an answer there, but the Class-path: element in the manifest only seems to refer to .jar files that are not in the .jar (and not .properties files that are!) Questions: Is there any way to wrap the .properties file into the .jar file so that my user doesn't have to know it is there? 我找到了一个关于.jar文件的Manifest文件的Oracle站点,因为我希望那里有答案,但清单中的Class-path:元素似乎只是引用.jar中不存在的.jar文件(而不是.properties文件!)问题:有没有办法将.properties文件包装到.jar文件中,以便我的用户不必知道它在那里? Is there any way to wrap the Oracle driver .jar into the app's .jar so my user doesn't have to know it is there (Oracle says this needs 'custom code')? 有没有办法将Oracle驱动程序.jar包装到应用程序的.jar中,这样我的用户就不必知道它在那里(Oracle说这需要'自定义代码')?

TIA TIA

One of the appraoch can be to use the -D switch to define a system property on a java command line. 其中一个appraoch可以使用-D开关在java命令行上定义系统属性。 That system property may contain a path to your properties file. 该系统属性可能包含属性文件的路径。

Eg 例如

java -cp ... -Dmyproject.properties=/path/to/my.app.properties my.package.App java -cp ... -Dmyproject.properties = / path / to / my.app.properties my.package.App

Fetch the property in your code as mentioned here: 如下所述,在代码中获取属性:

String propPath = System.getProperty( "myproject.properties" );

final Properties myProps;

final FileInputStream in = new FileInputStream( propPath );

 try
 {
     myProps = Properties.load( in );
 }
 finally
 {
     in.close( );
 }

Well, I would recommend to encrypt the sensitive data (username, password, url in this case) with public and private keys rather than hiding it. 好吧,我建议用公钥和私钥加密敏感数据(在这种情况下是用户名,密码,网址)而不是隐藏它。 It is afterall not hard to deflate any jar file (which is essentially a zip format) and trace the .properties file 现在不难对任何jar文件(基本上是zip格式)进行.properties并跟踪.properties文件

You can get the resources in your classpath (even when sealed in the JAR) by using the ClassLoader#getResource() and ClassLoader#getResourceAsStream() methods. 您可以使用ClassLoader#getResource()ClassLoader#getResourceAsStream()方法获取类路径中的资源(即使密封在JAR中)。

For example: 例如:

Properties values = new Properties();
values.load(ThisClass.class.getClassLoader().getResourceAsStream("myproject.properties"));
// umm, don't forget to close the stream, this code is just an example usage

Note that storing the username and password to any database in a program is considered a heavy security risk . 请注意,将用户名和密码存储到程序中的任何数据库都被视为存在严重的安全风险

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM