I have a class which end up bundled in a jar.
This class (call it A) use a property file which is accessed via classA.classloader.getRessourceAsStream("META-INF/routes.conf")
there is no such file in the jar.
I want to define this file in a war which include this jar.
Is there a way to do so?
Put the file somewhere in the war where it will be on the classpath - root of the classpath would probably be easiest - (WEB-INF\classes) and access it the same way you are now, changing the path to the file to it's location relative to the root of the classpath.
Related
How to add directory to a classpath ? I am trying to register IGLOBAL Extension in spock.
Not sure about the below step what exactly I should be doing:
Extensions are not discovered automatically, you must create a special org.spockframework.runtime.extension.IGlobalExtension file under META-INF/services directory on the CLASSPATH (of course it can be in a different JAR). The content of that file is simply a fully qualified name of the extension class
I am not sure how to add the file on the classpath
I want to read a json file placed in resources/data/info.json in my webapp. Got to know that need to use Convertor.class.getResourceAsStream("\\WEB-INF\\classes\\data\\Address.json"); where Convertor is my util class for some reason am unable to read the file and i see the InputStream returning null and i see the below exception :
com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
at [Source: UNKNOWN; line: 1, column: 0]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4133)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3988)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3058)
at com.wf.hrca.util.Convertor.unMarshal(Convertor.java:84)
resources is a folder of your source project on your development machine. Tomcat doesn't care about how your source project looks like. And by the way, when you'll deploy the application in production, there won't be any source project on the machine.
All Tomcat cares about is the war file you deploy. Inside this war file, the WEB-INF/classes directory, along with all the jar files under WEB-INF/lib, constitute the classpath of the application.
Convertor.class.getResourceAsStream(), as the javadoc explains (but you really need to read it to know) expects a /-separated path. If the path starts with a /, then the path starts at one of the roots of the classpath (i.e. / refers to WEB-INF/classes, and to the root of each jar file of WEB-INF/lib).
So, find where the json file is located inside the war file. If it's under /WEB-INF/classes/data/info.json, then you should use
Convertor.class.getResourceAsStream("/data/info.json");
I'm working on a spring-mvc project and was wondering if, like grails, I can create an external configuration file in tomcat with the appconfig folder. My project lives in /var/lib/tomcat7/webapps/<app> and was wondering if placing a configuration file in /var/lib/tomcat7/appconfigs/<config.xml> would work? If so, is it like grails and the application searches that location by default, or do I need to specify where that configuration lives? Thanks
What do you mean by "external configuration file"? Would this config file be separate from the war file? Or would it be packaged along with war file?
If packaged along with war file, you can put it under src/main/resources folder and it should be automatically packaged and placed in classpath.
If not packaged with war file, I usually put the configuration parameters under Tomcat's context.xml. Here's the documentation: http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Environment_Entries
In the manifest file for an eclipse plugin its possible to add jar files and
folders to the classpath (on the Runtime tab).
In the root of my plugin I have a folder lib containing a-1.0.1.jar, b-1.0.0-SNAPSHOT.jar. But only when I select each jar separately:
Bundle-ClassPath: .,
lib/a-1.0.1.jar,
lib/b-1.0.0-SNAPSHOT.jar
...can they be used inside my project. Why is it not possible to add them to the classpath by adding the common root folder only:
Bundle-ClassPath: .,
lib/
?
No, you can't. Eclipse is based on OSGi, which is the platform providing MANIFEST.MF support to build plugins.
When you set values under Bundle-ClassPath, OSGi search into each one to find class files. So you can put folders containing Java packages and class files. When you put a jar file, it is uncompressed in memory and viewed by OSGi as a regular folder, still searching for class files.
Unfortunately, there is no way to load all jar from a folder. No wildcard mechanism or something like that is allowed here.
I have created a composite component in JSF2. I works great.
I would like to create it as JAR for future use.
I followed the instructions here.
However, when it comes to CSS the browser refers to the location relatively to the project that uses the jar and not to the Jar location!
I defined it like this:
<h:outputStylesheet library="css" name="component.css" target="head" />
and I get this exception: GET http://localhost:8080/MY_APPLICATION/resources/component.css 404 (Not Found)
It's looking for it relatively to the projects, and not to the Jar project!
How can I make it relative to the JAR project?
EDITED
The JAR tree is:
META-INF
--resource
-- components
myComp.xhtml
components.css
-- img
-- scripts
--components.taglib.xml
--faces.config.xml
The war is a regular dynamic project:
WEB-INF
--lib
myJar.jar
-- web.xml
-- faces-config.xml
testComp.xhtml
Your JAR directory structure should be:
META-INF
--resources
-- components
myComp.xhtml
-- css <-- The library defined in the stylesheet
components.css <-- A stylesheet resource in the library
-- img
-- scripts
--components.taglib.xml
--faces.config.xml
Since, you are specifying the library name as css in the h:outputStylesheet tag with the resource name as component.css, the file should be present in a directory named css located in META-INF/resources directory of the JAR file.
Also, consider using a library name that is not bound to conflict with other names, if you intend to allow other developers to use your JAR.
AFAIK, the resource needs to be located in the same directory as the composite component. Have you tried to put the css in the same library?