Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > BOOK: Beginning Spring
|
BOOK: Beginning Spring
This is the forum to discuss the Wrox book Beginning Spring by Mert Caliskan, Kenan Sevindik; ISBN: 978-1-118-89292-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Spring section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2015, 08:41 PM
Registered User
 
Join Date: Apr 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question compile error on ch2 example

Hi,
I have a problem with the example in ch2, using java-base configuration. Most of the code doing well. However, in the main class, there is a annotationConfigApplicationContext class which is not define before. What should I do with it?

Thanks.
 
Old September 26th, 2015, 09:55 AM
cls cls is offline
Registered User
 
Join Date: Sep 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same error here

Hello, I have the same problem with all Configuration-Examples:

- java-based-configuration
- xml-based-configuration
- annotation-based-confiuration

I compiled the project by typing "mvn compile". After typing "java com.wiley.beginningspring.ch2.Main" i get the following error-message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
at com.wiley.beginningspring.ch2.Main.main(Main.java: 8)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support.ClassPathXmlAp plicationContext
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 24)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 57)
... 1 more

Can anyone help?
 
Old September 27th, 2015, 01:03 PM
Registered User
 
Join Date: Sep 2015
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by cls View Post
Hello, I have the same problem with all Configuration-Examples:

- java-based-configuration
- xml-based-configuration
- annotation-based-confiuration

I compiled the project by typing "mvn compile". After typing "java com.wiley.beginningspring.ch2.Main" i get the following error-message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
at com.wiley.beginningspring.ch2.Main.main(Main.java: 8)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support.ClassPathXmlAp plicationContext
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 24)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 57)
... 1 more

Can anyone help?
I think you are trying to run the Main from command line and in that case you probably didn't specified required Spring dependencies via java -classpath or -cp options. As there are usually several dependencies required in the classpath, you have to list all the dependencies while running the Main. Trying to run sample codes via command line might be a bit tedious task in the end. I would suggest you to use an IDE like Eclipse with maven installed of course in order to easly run the code samples. I just downloaded sample code zip file, extracted it, imported it into Eclipse and was able to run without any problem.

If you are still determined to run via command line, you can run maven dependency:copy-dependencies goal first, in order to download and copy those required dependencies into target/dependencies folder. Second, you need to list all those dependencies in you java -classpath or -cp line. I tried this as well, and sample code worked again, but I noticed another problem releated with maven compile goal not-copying non-java resources located in src/main/java source folders. You will also need to create a src/main/resources folder and copy resources files e.g xml bean configuration files into that source folder while preserving their corresponding package structure in that source folder as well.
 
Old September 27th, 2015, 01:08 PM
Registered User
 
Join Date: Sep 2015
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by waitingduck View Post
Hi,
I have a problem with the example in ch2, using java-base configuration. Most of the code doing well. However, in the main class, there is a annotationConfigApplicationContext class which is not define before. What should I do with it?

Thanks.
Do you mean following example? If yes, AnnotationConfigApplicationContext class is already imported, and is used to create a Spring ApplicationContext instance. I couldn't figure out what is the exact problem you faced here. Could you provide a little bit more information about it?

package com.wiley.beginningspring.ch2;

import org.springframework.context.annotation.AnnotationC onfigApplicationContext;

public class Main {

public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Ch2BeanConfigur ation.class);
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());

accountService.transferMoney(1, 2, 5.0);

System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());
}

}





Similar Threads
Thread Thread Starter Forum Replies Last Post
BOOK ERROR CH2 Returning Results from an Intent Gink BOOK: Beginning Android Application Development 0 January 11th, 2012 07:40 PM
ch2 error antorious BOOK: Building PHP Applications with Symfony, CakePHP, and Zend Framework 0 July 14th, 2011 11:24 PM
Ch2, pg30 - page_load error -Sequence contains no elements joefoos BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 2 June 2nd, 2009 12:32 PM
CH2 p54-55 parse error. nodster BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 July 25th, 2007 11:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.