2

My spring context file has this:

<context:property-placeholder location="classpath:web.properties" />

and I reference the key/values like:

<property name="username" value="${dataSource.username}"/>

My file layout is like:

>/www/site/app.war
>/www/site/configs/web.properties

And I run my app using:

>java -cp "/www/site/configs/*.*" -jar app.war

But then I get this exception:

java.io.FileNotFoundException: class path resource [web.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)

Even though I added the files to my classpath, it still can't find it why?

Update

So my config files are here:

/www/site/configs/web.properties
/www/site/configs/log4j.properties

I tried this:

>/www/site/java -cp ".:app.war:/www/site/configs/*.*" com.abc.server.MyServer

and:

>/www/site/java -cp ".:app.war:/www/site/configs/web.properties:/www/site/configs/log4j.properties" com.abc.server.MyServer

and even:

/www/site/java -cp ".:app.war" com.abc.server.MyServer

I get the same error:

2013-04-25 01:19:28.210:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2013-04-25 01:19:28.294:INFO:oejw.WebInfConfiguration:Extract jar:file:/www/site/app.war!/ to /www/site/work/app
2013-04-25 01:19:32.814:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/www/site/app/},file:/www/site/app.war
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2013-04-25 01:19:33.585:INFO:/:Initializing Spring FrameworkServlet 'app'
2013-04-25 01:19:35.750:WARN:/:unavailable
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/web-context.xml]: Could not resolve placeholder 'dataSource.url' in string value [${dataSource.url}]
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)

My web.properties has:

dataSource.url=jdbc:mysql://localhost/appdb
dataSource.username=root
dataSource.password=123

Again my web-context.xml file has:

..
 <context:property-placeholder location="classpath*:/web.properties" />

    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="${dataSource.url}"/>
        <property name="username" value="${dataSource.username}"/>
        <property name="password" value="${dataSource.password}"/>
..

I also tried the following variations:

<context:property-placeholder location="classpath:web.properties" />
<context:property-placeholder location="classpath*:web.properties" />
<context:property-placeholder location="classpath*:/web.properties" />
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

2 Answers2

4

I have also faced similar issues. I cannot say why but I have found -cp arguments not to behave when lugged along with -jar.

You can use,

EDIT

As per the dicussion and going through: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

The correct syntax should be:

In windows:

java -cp "app.war;/www/site/configs/" <Main Class file name>

In Linux:

java -cp "app.war:/www/site/configs/" <Main Class file name>
Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
  • I still get the file not found error for my web.properties file. – Blankman Apr 24 '13 at 02:59
  • Also change in spring config to: classpath*:web.properties, meanwhile I will create a sample app to test – Himanshu Bhardwaj Apr 24 '13 at 03:14
  • Just tested the local setup and it worked fine. I added the folder containing the properties file into classpath and tested, the same was detected, by spring. – Himanshu Bhardwaj Apr 24 '13 at 04:18
  • Wow, it doesn't work for me! I keep getting this error: `org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/web-context.xml]: Could not resolve placeholder 'dataSource.url' in string value [${dataSource.url}]` (my web.properties file has dataSource.url=.... defined) – Blankman Apr 25 '13 at 01:27
  • 1
    The reason is in the documentation. It specifically says that the -cp argument is ignored if the -jar option is used. – user207421 Apr 25 '13 at 02:58
  • @EJP he isn't using the -jar switch though? – Blankman Apr 25 '13 at 03:47
  • You can consider writing the correct command as an edit to the question. So people can refer it easily. – Himanshu Bhardwaj Apr 25 '13 at 03:56
  • @Blankman I am referring to the second sentence of this answer in which he doesn't know why the `java -jar` command works as documented. – user207421 Apr 25 '13 at 04:23
  • Answer is not correct. A CLASSPATH is not a set of filenames. – user207421 Apr 25 '13 at 04:25
  • Updated the answer, hope its fine now. IDEs sometimes does a little more than what you expect it to. :) – Himanshu Bhardwaj Apr 25 '13 at 04:33
  • 2
    Why don't you just delete the wrong part of answer and leave the right part? Nobody's interested in the history, and if they are they can read the edits. – user207421 Apr 25 '13 at 04:37
2
  1. Items accessible via the CLASSPATH aren't necessarily files at all. They might be still in the JAR or WAR file. They should be accessed as resources, via Class.getResource() and friends.

  2. Entries in the CLASSPATH aren't files either. A CLASSPATH is one or more directories or JAR files inside which the resources are to be found, according to the package structure.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • not sur I follow you, if I put my log4j.properties on the classpath, will spring not pick that up because its not in the resources folder? I thought i could swap my .properties files easily but just adding them to my classpath. – Blankman Apr 25 '13 at 02:27
  • 1
    OK, a CLASSPATH doesn't contain file names. It contains the names of directories which are at the head of a package structure, or names of JAR files. *Naming* the properties file in the CLASSPATH doesn't add it to the CLASSPATH at all. – user207421 Apr 25 '13 at 03:00
  • So does that mean I should do: "/www/site/conf/*.*" ? – Blankman Apr 25 '13 at 03:02
  • @EJP one question here: If I am executing the command like: **java -cp "app.war;/www/site/configs/*.*"
    **, this should mean that /www/site/configs all files are included in classpath. And resolving a resource like: in spring should resolve it as a resource. Am I correct?
    – Himanshu Bhardwaj Apr 25 '13 at 03:42
  • @HimanshuBhardwaj No you're not. I've just explained that. Again: a classpath is one or more directories or JAR files. **Not** a set of filenames. – user207421 Apr 25 '13 at 04:21
  • So you mean to say that for -cp we should rather say -cp "app.war;/www/site/configs/" and this should work (removed *.*) part. – Himanshu Bhardwaj Apr 25 '13 at 04:24
  • @HimanshuBhardwaj I said what I meant to say. I stated the general principle. Your conclusion follows directly – user207421 Apr 25 '13 at 04:36