在运行时通过 java 命令提示符加载配置文件 (project.properties)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22835800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:13:31  来源:igfitidea点击:

load config file (project.properties) at runtime via command prompt in java

javacmdproperties-file

提问by deepaksharma

I would like to load properties file via command prompt in java.

我想通过 java 命令提示符加载属性文件。

Properties file name: project.properties

属性文件名:project.properties

java -classpath .;test.jar; com.project.Main

What 'll be the command if I'll load the properties files via command prompt.

如果我通过命令提示符加载属性文件,那么命令是什么。

Thank in advance.

预先感谢。

I have executed the below mentioned command on command prompt but not get any output.

我在命令提示符下执行了下面提到的命令,但没有得到任何输出。

java -classpath .;test.jar; -DPROP_FILE="C:\Program Files\DemoApp\config\project.properties" com.project.Main

java -classpath .;test.jar; -DPROP_FILE="C:\Program Files\DemoApp\config\project.properties" com.project.Main

回答by Petr Mensik

Run java -classpath .;test.jar; com.project.Main project.properties, than read this argument in your main method and load the file.

运行java -classpath .;test.jar; com.project.Main project.properties,而不是在您的主要方法中读取此参数并加载文件。

 public static void main(String[] args) {
     String fileName = args[0];
     Properties prop = new Properties();
     InputStream in = getClass().getResourceAsStream(fileName);
     prop.load(in);
 }

回答by sprabhakaran

Send file path as below format,

发送文件路径如下格式,

java -classpath .;test.jar; -DPROP_FILE=conf\project.properties com.project.Main

Use below code for getting property file

使用以下代码获取属性文件

String propFile = System.getProperty("PROP_FILE");
Properties props = new Properties();
props.load(new FileInputStream(propFile));

Thanks.

谢谢。

回答by vishal

java -classpath c:\java Client test.properties 

"c:\java" is classpath - change to your java classpath

"c:\java" 是类路径 - 更改为您的 Java 类路径

回答by Adriaan Koster

In your code load it as a java.util.ResourceBundle:

在您的代码中将其加载为java.util.ResourceBundle

ResourceBundle properties = ResourceBundle.getBundle("project");

You can access the properties via the ResourceBundle API.

您可以通过ResourceBundle API访问这些属性。

Put your properties file on the classpath when starting your app:

启动应用程序时将属性文件放在类路径上:

java -classpath .;test.jar;project.properties com.project.Main