FileNotFoundException..Classpath resource not found in spring? - spring

I have code like this in Main.java :
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Until recently it was working, but I don't know why it started failing with the below exception:
Exception in thread "main"
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from
class path resource
[spring-config.xml]; nested exception
is java.io.FileNotFoundException:
class path resource
[spring-config.xml] cannot be opened
because it does not exist
the spring-config.xml is in src/main/resources folder.
Actually I wanted to learn about the annotations: #Postconstruct and #Predestroy, so I changed the build path to Jdk 1.6 from Jdk 1.5.
Since then the problem started...
Any clue why it is not working?
NOTE: If any wants to see my project structure please follow this link
http://code.google.com/p/javapracticeram/source/browse/trunk/SpringExample/
EDIT:

Looking at your classpath you exclude src/main/resources and src/test/resources:
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
Is there a reason for it? Try not to exclude a classpath to spring-config.xml :)

Check the contents of SpringExample/target/classes. Is spring-config.xml there? If not, try manually removing the SpringExample/target/ directory, and force a rebuild with Project=>Clean... in Eclipse.

This is due to spring-config.xml is not in classpath.
Add complete path of spring-config.xml to your classpath.
Also write command you execute to run your project. You can check classpath in command.

Two things worth pointing out:
The scope of your spring-context dependency shouldn't be "runtime", but "compile", which is the default, so you can just remove the scope line.
You should configure the compiler plugin to compile to at least java 1.5 to handle the annotations when building with Maven. (Can also affect IDE settings, though Eclipse doesn't tend to care.)
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
After that, reconfiguring your project from Maven should fix it. I don't recall exactly how to do that in Eclipse, but you should find it if you right click the project node and poke around the menus.

I was getting the same problem when running my project. On checking the files structure, I realised that Spring's xml file was inside the project's package and thus couldn't be found. I put it outside the package and it worked just fine.

Best way to handle such error-"Use Annotation".
spring.xml-<context:component-scan base-package=com.SpringCollection.SpringCollection"/>
add annotation in that class for which you want to use Bean ID(i am using class "First")-
#Component
public class First {
Changes In Main Class**-
ApplicationContext context =
new AnnotationConfigApplicationContext(First.class);
use this.

Related

Can I import resources like FTL files from another Maven project in my Liferay portlet?

We have several Liferay 6.2 portlets in different Maven projects. Some of these are imported as dependencies by others. While this allows me to access the java classes, I can't figure out how to import Freemarker files from one project in another project.
I assume I would need to access the resources from the dependency project, and then tell Freemarker how to find and include them. Assuming that's true, that leaves me with two questions:
How can I access resources, like FTL files and images, from another project that is list as a dependency in the Maven pom file of my Liferay project, especially in the server-side code?
How can I tell Freemarker where to look for the FTL files in the project that is listed as a dependency in the Mavan pom file?
If I'm wrong about what I need to be doing, then what is the correct way to give Freemarker access to the FTL files?
EDIT: I have a way around the first problem (though it's not a great way, I feel there's probably a better solution). I've tried to set up a Freemarker configuration using the path of the external FTL files, but I don't really know what to DO with the configuration; it doesn't look like it's actually being used.
I eventually found this question:
Maven: Extract dependency resources before test
Using the maven-dependency-plugin code in the original question and in the answer, I was able to copy the FTL files from the dependency into the Freemarker folder of the child project (in the compiled target folders of course), allowing them to be imported by Freemarker without any other changes made to the child project (the FTL files had to be moved to the resources directory in the parent project). Here are the relevant sections of the child project pom file:
I included the parent project in the dependencies:
<dependency>
<groupId>ourGroupId</groupId>
<artifactId>parentArtifactId</artifactId>
<version>1.0.0</version>
</dependency>
And then added the maven-dependency-plugin in the plugins section of the build, using the same artifact id I used in the dependency section:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>parentArtifactId</includeArtifactIds>
<includes>freemarker/*/*.ftl</includes>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I chose the phase and goals that seemed to make sense, I'm not sure if they're "optimal" but they seem to work. The FTL files are inside a sub-folder, under the "freemarker" folder, in the resources directory of the parent file. The "includes" parameter is a bit more restrictive that I thought, it seems I need to have the correct folder depth specified in order to import the files correctly, but that's fine.
Just for clarity, I made sure the resulting location of the copied Freemarker files matched the Freemarker that folder had already been specified in the applicationContext.xml file:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
</bean>

WildFly Maven Plugin - Setting Blank Context Root

I am using the Maven WildFly plugin and have the following in my pom.xml (version is 2.0.1.Final and path points to a local WildFly 8.2.1.Final server).
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${wildfly.version}</version>
<configuration>
<jboss-home>${wildfly.path}</jboss-home>
</configuration>
</plugin>
</plugins>
</build>
The application is getting deployed as localhost:8080/myArtifactId-myVersion which is expected as the default behavior is to use the WAR name. However, I want to change the context root so that the application is accessible via localhost:8080/ (i.e. I want a blank context root).
Method 1: Change the Pom Configuration
I can change the finalName build setting in my pom.xml as follows.
<build>
<finalName>newContextRoot</finalName>
</build>
This correctly updates the url to localhost:8080/newContextRoot. I then tried to change it to be a blank value.
<build>
<finalName></finalName>
</build>
However, this results in a Value must not be empty error in my IDE and when I try to build it fails, saying Error assembling WAR: A zip file cannot include itself.
Method 2: Change Plugin Configuration
Probably the better solution is to change the configuration of the Maven WildFly plugin itself. Under the <configuration> section, I can add the following.
<name>anotherContextRoot.war</name>
This correctly changes the url to localhost:8080/anotherContextRoot. So then I tried again to create a blank name as in the following.
<name>.war</name>
However, this results in an IllegalArgumentException: Empty name segment is not allowed for module.
Question
Does anyone know the appropriate way to make the context root blank? Thanks in advance.
I managed to figure this out. You do not need to do anything except add a jboss-web.xml file into the WEB-INF directory as follows.
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/</context-root>
<default-encoding>UTF-8</default-encoding>
</jboss-web>
This sets the context root and overrides what the plugin was doing.
Hope this helps someone else.
If you save your war with the name ROOT.war and deploy it, WildFly will configure it as root application.

Netbeans 8 won't reload static Thymeleaf files

I am using Spring Boot and Thymeleaf via Maven. I can't seem to get Netbeans to automatically re-deploy any of my Thymeleaf template files when I make changes. In order to see the changes I need to do a full clean/build/run. This takes way too long.
The templates are in src/main/resources/templates. I have an application.properties file in src/main/resources/ with spring.thymeleaf.cache=false and spring.template.cache=false.
I have "Compile on save", "Copy resources on save" and "Deploy on save" turned on in the project settings.
My maven build produces a war file that Netbeans deploys to Tomcat and I am using the annotation #EnableAutoConfiguration.
Netbeans does hot deploy changes to the Java classes but not for any of the static files in src/main/resources/.
Software in use:
Mac OS X 10.9.4
Java 1.8
Netbeans 8.0.1
Tomcat 8.0.12
Spring Boot 1.1.7
Thymeleaf 2.1.3 (via Spring Boot)
Any guidance is much appreciated.
An option would be to look into configuring Thymeleaf's FileTemplateResolver
To do that with Spring Boot, define a bean implementing the ITemplateResolver interface with the name defaultTemplateResolver, when present, Spring Boot would take it instead of its default, here is how that would be done, and assuming you have component scanning active so this configuration class will be picked up automatically:
#Configuration
public class ThymeleafConfiguration {
#Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(".html");
resolver.setPrefix("path/to/your/templates");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
return resolver;
}
}
The prefix should be a relative path that when added to your runtime working directory (cwd), would resolve to the templates directory. If you are unsure, set that to the full absolute path, but then there would be no point of the above bean. Since setting the spring.thymeleaf.prefix property to an absolute path would probably have the same effect.
Have been looking for a solution to my eclipse+thymeleaf+sprint boot reloading templates dynamically for a log while....
Finally I found this question here and spring.thymeleaf.cache=false and spring.template.cache=false fixed my issue.
Besides setting the Thymeleaf views as non-cacheable by ie. spring.thymeleaf.cache=false in your application.properties,
try explicitly defining the resource directory in your pom.xml:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
...
</build>
To deal with that, the spring-boot-maven-plugin in the pom.xml should seems like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
And add this to your application properties:
spring.thymeleaf.cache=false
It usually works to Spring beans too.
Just to say this works nicely for me using an external instance of Tomcat:
Run Tomcat with JRebel or Spring Loaded javaagent as a VM option
Turn off "Compile on save", "Copy resources on save" and "Deploy on save"
Add a custom action in Netbeans that executes the compile goal
Run that when you want to see an update
http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions
https://github.com/spring-projects/spring-loaded
https://zeroturnaround.com/software/jrebel/quickstart/standalone/
Or you can you use the embedded tomcat with spring-boot-maven-plugin and Spring Loaded instead, then you won't need the compile action:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html
I had this problem too. I notice Netbeans reload automaticaly webpages that are in
/src/main/webapp/
You have to move all your templates from /src/main/resources/templates to this directory.
Also you have to change the spring boot property on application.properties file:
spring.thymeleaf.prefix=templates/
That works for me
I had this same issue on Netbeans 8.0.2 and Windows. I was building a WAR to be deployed to Tomcat, but I wanted to try out Spring Boot. It looks like newer versions of Netbeans might resolve this with the Spring Boot plugin or using Eclipse. It seemed nutty to swap IDEs over something tiny like this. I tried all of the suggestions I could find; spring loaded, caching properties, extending the TemplateResolver...I couldn't get any of them to work. I finally stumbled on this blog and following these instructions solved my issue.

An annotation processor exception

When i build project using maven and netbeans i got this exception : "java.lang.RuntimeException: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for org.jaxen.FunctionContext not found".
In this article people said that solution is to disable annotation processing in NB, but i don't have this checkbox into the build settings of my project.
Maybe someone knows how to solve this problem or how to disable annotation processing
As for disabling annotations processing part, it's controlled by -proc:none option for javac; thus you can set
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<proc>none</proc>
</configuration>
</plugin>
I was faced with this problem again and found other solution. I just remove "target" folder of my project and project was compiled successfully
If you disable annotations process maybe your project is not going to run on final deploy destination, the best way to solve the problem is downloading the jaxen library at
http://jaxen.codehaus.org/releases.html
And then import jaxen-*.jar into your project.
I had the same exception pop up when using poi and poi-ooxml dependencies. The root cause was that I had a private member of type XSSFCellStyle in one of my classes. Changing the type to CellStyle made this compile time exception go away.

Intellij: jboss-ejb3.xml entry keeps disappearing from ${PROJECT_DIR}/.idea/artifacts/XXX_war_exploded.xml

Why is it that although I've
set the proper path to the JBoss EJB deployment descriptor in my project's EJB facet
added jboss-ejb3.xml to Intellij's artifact Patrac-web:war exploded's <output root>/WEB-INF
that any time I make the simplest change to pom.xml Intellij removes the following entry from ${PROJECT_DIR}/.idea/artifacts/Patrac_web_war_exploded.xml:
<element id="file-copy" path="$PROJECT_DIR$/Patrac-ejb/src/main/resources/META-INF/jboss-ejb3.xml" />
and, as a result, jboss-ejb3.xml does not get copied to the target directory?
It's as though each time I make a change to pom.xml Intellij "reloads" the deployment configuration using the POM to override what settings I make within the IDE. Perhaps because I have no entry in my pom.xml for copying jboss-ejb3.xml from source directory to target directory the settings I make in Intellij IDE keep disappearing whenever Intellij "reloads." Pure conjecture on my part, but this is what seems to be happening.
If so, what change do I need to make to pom.xml in order to make this stop happening?
When a project is (re)imported from Maven IDEA configures it such way that when you invoke 'Build' from IDEA it produces the same result as Maven's 'package' goal. If you need to copy jboss-ejb3.xml to WEB-INF just put it under 'src/main/webapp/WEB-INF' directory and it will be copied by Maven and so do IDEA.
Here is an alternative to Nik's solution that I tried because I wanted to leave jboss-ejb3.xml in META-INF. Taking a look at the maven documentation, which shows how to treat jboss-ejb3.xml as a web resource and copy it into WEB-INF, I added the following to the maven-war-plugin configuration and the problem was resolved. Well, kinda sorta. But not really.
<webResources>
<resource>
<directory>../Patrac-ejb/src/main/resources/META-INF</directory>
<includes>
<include>jboss-ejb3.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
Although doing it this way eliminated the need to fiddle around with IDEA facet configuration settings (because Maven was configured to copy the file, not IDEA), a new problem was introduced: two copies of jboss-ejb3.xml appeared in the WAR, one in WEB-INF and the other inside the EJB JAR (in META-INF). Although there were no apparent consequences (the application ran just fine) I preferred not to have a duplicate copy of the descriptor located inside the EJB JAR.
Using the Maven EJB plugin documentation, I tried to add an exclusion to my maven-ejb-plugin configuration e.g.
<configuration>
<ejbVersion>3.1</ejbVersion>
<excludes>
<exclude>**/jboss-ejb3.xml</exclude>
</excludes>
</configuration>
This should have prevented the duplicate copy of jboss-ejb3.xml from appearing in the EJB JAR META-INF but it didn't work for me, and after fruitlessly googling various variations of "maven-ejb-plugin excludes not working properly" I gave up.
If I could have gotten the excludes to work then I would have preferred this solution over moving jboss-ejb3.xml into src/main/webapp/WEB-INF because although this solution is slightly more complex (it requires additional Maven configuration settings in two POMs), the EJB-related descriptor would remain in the EJB module.
I've decided to go with Nik's solution until I can resolve the excludes problem.

Resources