简体   繁体   English

如何使用命令行将属性文件添加到 java 类路径?

[英]How to add properties file to java classpath using command-line?

My current structure looks like我目前的结构看起来像

$ls
project.jar
propertiesFileA
propertiesFileB

The way I run my project, I do我运行我的项目的方式,我做

java -jar project.jar MyClass 

It's a Spring based project where context.xml resolves some of the properties from propertiesFileA and propertiesFileB这是一个基于Spring的项目,其中context.xml解析来自propertiesFileApropertiesFileB一些propertiesFileB

So I get error because Spring can not find these files.所以我收到错误,因为 Spring 找不到这些文件。 That means I need to add them to classpath .这意味着我需要将它们添加到classpath

I tried following with no success at all我试过完全没有成功

java -classpath .:/project.jar MyClass

java -cp "project.jar:." MyClass

java -classpath .;project.jar MyClass

Question

How can I provide propertiesFileA and propertiesFileB on command-line to MyClass ?如何在命令行上向MyClass提供propertiesFileApropertiesFileB

Regarding your question title, "How to add properties file to java classpath using command-line?", to be specific, you need to add to the class path the directory that contains your properties files.关于您的问题标题,“如何使用命令行将属性文件添加到 java 类路径?”,具体而言,您需要将包含属性文件的目录添加到类路径中。

For example, if your properties files are located at例如,如果您的属性文件位于

  • /path/to/your/properties/files/file1.properties /path/to/your/properties/files/file1.properties
  • /path/to/your/properties/files/file2.properties /path/to/your/properties/files/file2.properties

Then what you need is to add their directory (/path/to/your/properties/files/, or /path/to/your/properties/files) to the class path, such as:然后你需要的是将它们的目录(/path/to/your/properties/files/,或/path/to/your/properties/files)添加到类路径中,例如:

java -cp "/path/to/your/properties/files/:/path/to/your/project.jar:/path/to/3rdparty/jars/*" com.shn.ec.main.MyClass

java -cp "/path/to/your/properties/files:/path/to/your/project.jar:/path/to/3rdparty/jars/*" com.shn.ec.main.MyClass

The following helped me以下帮助了我

java -cp ".:project.jar:3rdparty/*" com.shn.ec.main.MyClass

I was missing FQCN when calling out class FQCN时我错过了FQCN

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

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