Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行配置文件位于不同目录中的java程序_Java_Spring_Classpath_Config - Fatal编程技术网

运行配置文件位于不同目录中的java程序

运行配置文件位于不同目录中的java程序,java,spring,classpath,config,Java,Spring,Classpath,Config,我正在运行一个java程序,我的程序在一个jar文件中,我已经创建了一个可执行jar,让我们把它命名为Test.jar 我的程序需要config.properties文件中的一些属性,我没有将它们包含在jar中,而是将它们保存在名为conf的目录中 现在我需要将这些配置文件添加到类路径中,这样程序在运行时就可以拥有这些属性 但问题是在我的程序执行过程中找不到属性文件,而引发文件未找到异常。我正在使用Spring5并使用注释@PropertySource{classpath:config.prop

我正在运行一个java程序,我的程序在一个jar文件中,我已经创建了一个可执行jar,让我们把它命名为Test.jar 我的程序需要config.properties文件中的一些属性,我没有将它们包含在jar中,而是将它们保存在名为conf的目录中 现在我需要将这些配置文件添加到类路径中,这样程序在运行时就可以拥有这些属性

但问题是在我的程序执行过程中找不到属性文件,而引发文件未找到异常。我正在使用Spring5并使用注释@PropertySource{classpath:config.properties,classpath:application.properties}读取配置 因此,理想情况下,如果我将配置放在类路径中,它应该是可读的

n、 b-我在使用eclipse运行程序时没有问题,因为我的属性文件位于资源目录中

我正在从命令行运行程序,如下所示- java-cp-Test.jar;conf*com.test.TestMain

我正在获取文件未找到异常

我的目录结构是TestCode目录,里面有conf dir和Test.jar文件,我是从TestCode目录运行的

我的maven构建结构如下所示,不确定是否与maven有关

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/log4j2.xml</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.TestMain</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins> 
</pluginManagement>
</build>
我得到的例外是

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class 
[com.test.AppConfig]; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
at com.test.TestMain.main(TestMain.java:19)
Caused by: java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73)
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:59)
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67)
at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37)
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:452)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)

这是在Windows还是Linux中运行的?因为-cp的分隔符对于Windows是分号,对于Unix是冒号。
您尝试过java-Dspring.config.location=/conf/-jar Test.jar吗?这可能更简单,因为您解释的是您的需要。

在@PropertySource中,将属性文件的位置指定为file:/path/to/config.propertiesThanks Madhu,但在这种情况下,我正在修复属性文件的路径,我的整个程序都在TestCode目录中,由conf dir和属性文件以及包含主类和其他类的jar文件组成,我想将该目录放在任何机器中,并从那里运行它,为此,我想使用classpath中的属性文件。您的意思是要将整个TestCode目录放在任何一台机器上,然后从那里运行jar吗?然后你需要一个可配置的@PropertySource,在这里你可以通过命令行arg传递conf文件的位置,同时使用java-jar启动应用程序,将其作为@PropertySourcevalue=${file.path}传递,并将file.path arg作为-Dfile.path=file:/path/to/config.propertiesThanks-Madhu传递,它起作用了,但是你知道-cp选项有什么问题吗?我正在运行这个程序windows@Avishek您是否尝试过上面提到的java-Dspring.config.location=/conf/-jartest.jar?