karansky
karansky

Reputation: 558

How to solve "Unhandled exception type BeansException"

I am a beginner at Spring Batch. I am following this guide to create a HelloWorld of Spring Batch. In the class with main method when I was trying to get Application Context by using new ClassPathXmlApplicationContext("..."), the IDE shows an error message saying

Unhandled exception type BeansException

I cannot solve that error even though I have a catch block that catches all types of exceptions. Refer to the code block below:

    public static void main(String args[]) {
        try {
            //error message appears here
            AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addString("Date", "12/02/2011");
            jobLauncher.run(job, builder.toJobParameters());

            JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());
            System.out.println(jobExecution.toString());
        } 
        catch(Exception e) {
            e.printStackTrace();
        } 
    }

Then, I tried to solve it by import org.springframework.beans.BeansException; and tried to catch BeansException. Although the unhandled BeansException error was solved but another error message appeared:

No exception of type BeansException can be thrown; an exception type must be a subclass of throwable

Refer to the code block below:

    public static void main(String args[]) {
        try {
            AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("simpleJob.xml");

            JobParametersBuilder builder = new JobParametersBuilder();
            builder.addString("Date", "12/02/2011");
            jobLauncher.run(job, builder.toJobParameters());

            JobExecution jobExecution = jobRepository.getLastJobExecution(job.getName(), builder.toJobParameters());
            System.out.println(jobExecution.toString());
        }
        //error message appears here
        catch(BeansException e) {
            //do something
        } 
        catch(Exception e) {
            e.printStackTrace();
        } 
    }

What is the correct way to solve this error?

Additional note: I do not have my own class named BeansException.

Edit: Stack trace (proceed with error option):

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No exception of type BeansException can be thrown; an exception type must be a subclass of Throwable

    at SpringBatchHelloWorld.BatchLauncher.main(BatchLauncher.java:29)

Upvotes: 8

Views: 10173

Answers (2)

Vipin Purohit
Vipin Purohit

Reputation: 1229

  1. delete existing jars files form .m2\repository\org\springframework folder.
  2. do clean build by using same version of spring-context.jar, spring-beans.jar and spring-core.jar

Upvotes: 0

karansky
karansky

Reputation: 558

Thanks to Ken Bekov's comments to the question, I was able to solve this problem and I formulated this solution to officially give this question an answer. Credits should be given to Ken Bekov.

Solution: The problem was due to different version of .jar files included in the build path. The .jar files that should be included are: spring-context-4.2.5.RELEASE.jar, spring-beans-4.2.5.RELEASE.jar and spring-core-4.2.5.RELEASE.jar (notice the same version number - 4.2.5).

As for spring-batch-core-3.0.6.RELEASE.jar, spring-batch-infrastructure-3.0.6.RELEASE.jar and others, they do not necessarily need to have the same version number (4.2.5).

After the correct .jar files were included, there won't even be an "Unhandled exception type BeansException" error message for new ClassPathXmlApplicationContext("...");

Upvotes: 9

Related Questions