Adding spring XML files to classpath (Windows cmd-line) - java

I am trying to run a jar file via cmd line that uses Spring and a spring xml configuration file.
The cmd line call is similar to:
java -cp lib/MyJar.jar my.package.MyClass
The error I get is:
Caused by: java.io.FileNotFoundException: class path resource
[myPath/mySpringCfg.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
My manifest classpath is similar to:
Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./myPath/
The call that loads the file equates to:
context = new ClassPathXmlApplicationContext("myPath/mySpringCfg.xml");
Is there a way to correctly pull in XML files in the classpath so that Spring will work as expected? It seems like the classpath docs only talk about archive files and folders.
Thanks!
UPDATE
It seems to run fine when I switch over to FileSystemXmlApplicationContext. I guess the ClassPathXmlApplicationContext cannot be used from command-line

Your reference to the XML is myPath/mySpringCfg.xml - this means that myPath has to be in the classpath.
Change your manifest to be:
Class-Path: 3rdPartyJar1.jar 3rdPartyJar2.jar ./
This way myPath will be a part of the classpath and not just its contents.
Note:
The application configuration XML is a part of your application's code, don't mistake it for a configuration.
If you want configuration - put it outside in a properties file and use place-holders in your XML configuration file.
Update:
I think the root cause of your problem is in the code (I didn't test it though) - try this instead:
context = new ClassPathXmlApplicationContext("/myPath/mySpringCfg.xml");
The difference is in the '/' before 'myPath'

I am not aware of the architecture of your project, but why not place your xml configuration file into your project jar?

Related

IgniteException : Spring XML configuration path is invalid

Getting the following exception in intelliJ.
Caused by: class org.apache.ignite.IgniteException:
Spring XML configuration path is invalid: example-ignite.xml.
Note that this path should be either absolute or a relative local file system path, relative to META-INF in classpath or valid URL to IGNITE_HOME.
How can i fix it?
Thanks
If your configuration bean's definition has abstract=true parameter, try removing it if it does.
I think, the problem is that example-ignite.xml file has only abstract IgniteConfiguration. This is the case in the default configuration file in examples.
I had this problem. my issue was due existing error in config.xml file. for test, i ignore config.xml from environment variable (-v) and run ignite without it and after i saw that it worked, i figured out that issue is cause that.
i worked with ignite in docker in linux.
I had this problem and fixed with fake solution, , i used config file by internet path and not local file system path. i set config.xml on one domain path (https://example.net/config.xml) and then i set this path for spring xml configuration path.

Add configuration directory to classpath using spring boot

I would like to add a configuration directory to the classpath for a spring boot application at start up, so it can load xml files from the configuration directory.
ie /var/application/config contains
test.xml, dev.xml
The xml will contain mapping information that is required by the application; this is different from application.properties.
I would like to load them at startup.
I am using ClassPathResource to load the files.
Please advise.
You can define your own classpath by the command-line. Lets suppose your jar is myapp.jar and you wand add one extra directory /var/application/config/, so you can execute with the following command line:
java -cp myapp.jar:/var/application/config/ -Dloader.main=myapp.Application org.springframework.boot.loader.PropertiesLauncher
ps: if you are using Windows use ; instead of : to separate your classpath items.
From the Spring Boot Reference Guide, add your config location:
java -jar myproject.jar --spring.config.location=classpath:/var/application/config/

using property-placeholder for in jar properties file

I use this line :
context:property-placeholder location="classpath*:resources/BLLresources/MQ.properties"
in a spring.xml file to look for MQ.properties which is inside the jar where the xml file is (in config folder at the root level).
I got error :
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: {{uriMesarimReadQueue}} due to: Properties file classpath*:resources/BLLresources/MQ.properties not found in classpath
If I put the properties file outside the jar it works fine.
the jar is in the classpath.
any advice ?
I think your have your references setup incorrectly. In a normal java project the resources folder would be located in src/main/resources, this folder gets mapped to the root of the generated jar file. Therefore, if you had a file src/main/resources/BLLresources/MQ.properties, the mapping would be classpath*:BLLresources/MQ.properties

Can't find bundle for base name /Bundle, locale en_US

I'm using a library that has a dependency on JSF.
When I try to run my project, it show following exception massage..
java.util.MissingResourceException: Can't find bundle for base name /Bundle, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1427)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1250)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:705)
Any ideas ?
The exception is telling that a Bundle_en_US.properties, or Bundle_en.properties, or at least Bundle.properties file is expected in the root of the classpath, but there is actually none.
Make sure that at least one of the mentioned files is present in the root of the classpath. Or, make sure that you provide the proper bundle name. For example, if the bundle files are actually been placed in the package com.example.i18n, then you need to pass com.example.i18n.Bundle as bundle name instead of Bundle.
In case you're using Eclipse "Dynamic Web Project", the classpath root is represented by src folder, there where all your Java packages are. In case you're using a Maven project, the classpath root for resource files is represented by src/main/resources folder.
See also:
Maven and JSF webapp structure, where exactly to put JSF resources
maven-tomcat-plugin
If you start the Project using the maven-tomcat-plugin / maven-tomcat7-plugin, you must place the Bundle.properties, or even the Resource.properties in src/main/webapp/WEB-INF/classes. Dont ask why, its because how the plugin fake a tomcat.
If you are running the .java file in Eclipse you need to add the resource path in the build path .
after that you will not see this error
In my case the problem was using the language tag "en_US" in Locale.forLanguageTag(..) instead of "en-US" - use a dash instead of underline!
Also use Locale.forLanguageTag("en-US") instead of new Locale("en_US") or new Locale("en_US") to define a language ("en") with a region ("US") - but new Locale("en") works.
I had the same problemo, and balus solution fixed it.
For the record:
WEB-INF\faces-config is
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<message-bundle>
Message
</message-bundle>
</application>
</faces-config>
And had Message.properties under WebContent\Resources (after mkyong's tutorial)
the pesky exception appeared even when i renamed the bundle to "Message_en_us" and "Message_en". Moving it to src\ worked.
Should someone post the missing piece to make bundles work under resources,it would be a beautiful thing.
In my case I was dealing with SpringBoot project and I got the same exception.
Solution is made by adding env.properties file into classpath (i.e. src/main/resource folder). What was making the issue is that in log4j configuration there was property like
<Property name="basePath">${bundle:env:log.file.path}</Property>
The Bundle.properties file must be in the directory of the .class files. If it is located in the src directory then you need to make sure to copy it to the bin (output) directory in the same package.
Bundle names have to be fully qualified, like if your bundle Bundle.properties
is inside x.y.z package, then you have to write ResourceBundle.getBundle("x.y.z.Bundle");
I had the same problem using Netbeans. I went to the project folder and copied the properties file. I think clicked "build" and then "classes." I added the properties file in that folder. That solved my problem.
I use Eclipse (without Maven) so I place the .properties file in src folder that also contains the java source code, in order to have the .properties file in the classes folder after building the project. It works fine.
Take a look at this post: https://www.mkyong.com/jsf2/cant-find-bundle-for-base-name-xxx-locale-en_us/
Hope this help you.
The problem must be that the resource-bunde > base-name attribute at the faces-config.xml file has a different path to your properties. This happened to me on the firstcup Java EE tutorial, I gave a different package name on then project creation and then Glassfish was unable to find the properties folder which is on "firstcup.web".
I hope it helps.
Make sure you didn't add the properties files in the wrong resources folder as there is one under 'Web Pages' and one under 'Other Sources/...'. They needed to be under 'Other Sources/...'.
I was able to resolve the issue, the resource was in my project directories but when the junit utility tries to load it, it was returning an error of MissingResourceException. And the reason was the resource was in the not on the classpath of the test class package so when I added the cfg/ folder to my classpath path entry in eclipse and set the output directory in the build conf to the same class package the issue was resolved.
When you try this approach just make sure, the classpath conf file shows the classpath entry of the resource directory (eg. cfg/)
In maven, folder resources, create the same package structure where the configuration files are located and copy them there

Setting log4j.properties file for logging in servlets

The server is a simple jetty Server
How to set the log4j.properties file i have a proper log4j properties file,
but while setting the log4j.properties
using the following manner, i have the log4j.properties in my src folder
PropertyConfigurator.configure("log4j.properties");
it works fine when i am working locally, but when i create a jar file and run its throwing an exception like java.io.FileNotFoundException:
i have tried extracting it and created it in another folder called resources and tried accessing that by the following method
PropertyConfigurator.configure("resources/log4j.properties");
even after that its showing the same error
how to export the entire project as a jar file and make this log4j problem to work?
Found another link
Log4j Properties in a Custom Place
and in that it is required to set the class path
java -Dlog4j.configuration=conf/log4j.properties -classpath ...
Do not know how to set the -classpath and dont know whether this method will work!!
And even if its exported as a jar file it should work!
If the log4j.properties resource directory is on the classpath, you could use:
PropertyConfigurator.configure("classpath:resources/log4j.properties");
To see the working directory for Jetty, you could add:
System.out.println(System.getProperty("user.dir"));
before the PropertyConfigurator.configure statement. This would allow you to see where the property file is located in relation to the server's working directory.
In order to make it work immediatley, you can configure them from code:
Properties props = new Properties();
props.setProperty("<KEY>","VALUE");
PropertyConfigurator.configure(props);
Hardcode the props object with all the properties from log4j.properties file.
This is not the solution you ask, but it might very helpful if you are short on time.

Categories

Resources