0

I am a beginner just started learning Spring Batch. I followed this tutorial here to create a helloworld example. While I was following the tutorial, I encountered a problem when I was trying to import org.springframework.batch.item.ItemProcessor into a java class. Hence I searched online and found out that I need to add something inside the build.gradle. The problem was, even though I have added the org.springframework.batch:spring-batch-core dependency in build.gradle, I was still having error message for the import. I am using Eclipse Java EE IDE 4.5.0 (Mars) to do this tutorial and have installed the Spring Test Suite (by following the tutorial). I have also tried right clicking the project and choose the "refresh" option but the error message is still there.

Additional note: I have also tried to follow the answer stated here but I cannot find the "Refresh Dependencies" in the "Gradle" context menu. How do you even know that the build.gradle is working?

Below is my code (Java class) that imports the Spring Batch:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//error message: "the import org.springframework.batch cannot be resolved"
import org.springframework.batch.item.ItemProcessor; 


//error message: "ItemProcessor cannot be resolved to a type"
public class PersonItemProcessor implements ItemProcessor<Person, Person> {

    @Override
    public Person process(final Person person) throws Exception {
        //some codes
    }
}

Below is the build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-consuming-rest'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.batch:spring-batch-core") //added by me
    compile("org.springframework.boot:spring-boot-starter")
    compile("org.springframework:spring-web")
    compile("com.fasterxml.jackson.core:jackson-databind")
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}
Community
  • 1
  • 1
karansky
  • 538
  • 1
  • 4
  • 24
  • open terminal/cmd, cd into the project directory in which the build.gradle file resides and type "gradle build" – neal Mar 10 '16 at 05:54
  • @neal i have the error " 'gradle' is not recognized as an internal or external command, operable program or batch file. " – karansky Mar 10 '16 at 06:04
  • I would download the binary from http://gradle.org/gradle-download/ and unzip it into a directory then follow installation instructions here: https://docs.gradle.org/current/userguide/installation.html – neal Mar 10 '16 at 06:08
  • when I installed gradle on my home computer (PC) i had to create a gradle.properties file in the same directory as my gradle.build file and add the following line. It did not work otherwise: org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_40 – neal Mar 10 '16 at 06:14
  • @neal Ok. I'm trying to follow the guide in the link that you have provided. I'll update here whether it works or not. Thanks! – karansky Mar 10 '16 at 06:18
  • @neal I've tried the user guide but I encountered a "could not find tools.jar error". In the end, I gave up on gradle and straightly downloaded the jar file that contains the library [here](http://mvnrepository.com/artifact/org.springframework.batch/spring-batch-infrastructure/2.2.0.RELEASE) and put it inside the build path. The error message is gone but I wonder if this is a good method – karansky Mar 10 '16 at 07:19
  • that stinks. I'm pretty sure I had to that's why I had "to create a gradle.properties file in the same directory as my gradle.build file and add the following line. It did not work otherwise: org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_40." I think gradle was trying to use my JRE rather than my JDK so I had to set the property above in order for it to find the JDK - and tools.jar - which is located in path/to/jdk/libs directory. – neal Mar 10 '16 at 07:24

1 Answers1

0

I had the same problem with the same tutorial except that I use IntelliJ instead of Eclipse. On IntelliJ there is a "Gradle" panel on the right side that you can open and click refresh. This will refresh your gradle. It solved the problem in my case. (Rebuilding gradle.build didn't)

Red arrow pointing to the gradle panel

Red arrow pointing to the refresh icon

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mate
  • 1
  • 2