简体   繁体   English

尝试在Java .war之外读取.properties文件时出错

[英]Error when trying to read a .properties file outside java .war

I am building a Java Web Application which lets users upload some files to the server. 我正在构建一个Java Web应用程序,该应用程序允许用户将一些文件上传到服务器。 Since the .war file may be deployed in servers having different file system hierarchies, I want to provide a .properties file and let the server admin define the location to save these files during initial system configuration. 由于.war文件可能部署在具有不同文件系统层次结构的服务器中,因此我想提供一个.properties文件,并让服务器管理员定义在初始系统配置期间保存这些文件的位置。 My application would then read this .properties file to determine where to store the uploaded files. 然后,我的应用程序将读取此.properties文件,以确定将上传的文件存储在何处。

While searching for a way to do this I came across a similar question on Stack Overflow and tried to follow the instructions give in the following answer. 在寻找一种实现方法时,我在Stack Overflow上遇到了类似的问题,并尝试遵循以下答案中的说明。
https://stackoverflow.com/a/8941774 https://stackoverflow.com/a/8941774

I wanted to try out if this solution would work. 我想尝试一下此解决方案是否可行。 So I edited my /usr/share/tomcat8/conf/context.xml file and included the following between the <Context> </Context> tags 因此,我编辑了/usr/share/tomcat8/conf/context.xml文件,并在<Context> </Context>标记之间添加了以下内容

<Environment name="config" value="/home/nilushan/temp/"  
     type="java.lang.String" override="false"/>

I created a file named yourfile.properties in /home/nilushan/temp and also copied the given Java code and executed it. 我在/ home / nilushan / temp中创建了一个名为yourfile.properties的文件,还复制了给定的Java代码并执行了它。

However I kept getting "exception in jndi lookup" message. 但是我一直收到“ jndi查找异常”消息。 After changing the code to print the exception, this is what I get 更改代码以打印异常后,这就是我得到的
"exception in jndi lookup:javax.naming.NameNotFoundException: Name [config] is not bound in this Context. Unable to find [config]." “ jndi lookup中的异常:javax.naming.NameNotFoundException:此上下文中未绑定名称[config]。无法找到[config]。”

From the error message I understand that this has something to do with not being able to read what is in the context.xml file and nothing to do with yourfile.properties file. 从错误消息中我了解到,这与无法读取context.xml文件中的内容有关,而与yourfile.properties文件无关。

Can someone please tell me what I am doing wrong to get this error and how I can fix it? 有人可以告诉我我在做什么错了,如何解决此错误?

Well a good solution to the problem is using config server. 一个很好的解决方案是使用配置服务器。 When you set it up you can direct your properties file to take it's configuration from it. 设置它后,您可以指导您的属性文件从中获取其配置。 It is quite easy to set up, here is a link to the documentation(it's quite short so you should understand it very fast) link . 设置非常容易,这是指向文档的链接(它很短,因此您应该很快理解它) 链接 You can also use it for other war/jar files. 您也可以将其用于其他war / jar文件。

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

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