1

I am reading a book "Introducing Spring Framework" and trying to run the first basic Hello World example.

Here Gradle is used to build project but somehow it fails to compile:

Caused by: java.lang.RuntimeException: Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

The problem is, that I have properly configured JAVA_HOME, and java -version properly returns the version of jdk.

What might be wrong?

Edit: I am using Linux. Ubuntu.

Edit 2: Both which javac and which java display /usr/bin/java Edit 3: Now the compiler seems to be working, but it doesn't understand spring annotation used in code. I do have spring reference in gradle.build:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = System.getProperty("mainClass")

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework:spring-context:4.0.5.RELEASE'
}
so_user
  • 333
  • 3
  • 15
  • 1
    Just a hunch - in Windows, if I change system variables, I have to quit my current "cmd" prompt and open another one for it to pick up changes... It's not that is it? Try "echo %JAVA_HOME%" maybe? Make sure you do not have multiple locations on your system path for the java.exe command - actually, ignore me, it's probably a gradle configuration thing... – BretC Jan 29 '15 at 16:03
  • I am using Ubuntu... – so_user Jan 29 '15 at 16:14
  • what does `which javac` and `which java` output? Otherwise you can also set the java.home in the gradle properties file. `org.gradle.java.home=/opt/jdk1.8.0_20` for example – mjspier Jan 29 '15 at 16:20
  • 2
    check [this](http://stackoverflow.com/a/22309017/1910582) and [this](http://stackoverflow.com/a/21212790/1910582) – Bond - Java Bond Jan 29 '15 at 16:25
  • @Java Bond - thank you - first link helped. Now I have further issue though – so_user Jan 30 '15 at 14:19
  • Now the compilation fails because I have used annotations and the compiler doesn't understand them - even though I do have the imports. Looks like gradle doesn't download the dependencies properly, or maybe wrong version? – so_user Jan 30 '15 at 14:26
  • Apparently Gradle uses different version of jdk in my case 1.6 instead of 1.7 from the PATH variable. I have tried setting the jdk manually after reading [link](http://stackoverflow.com/questions/18487406/gradle-how-to-tell-gradle-to-use-specific-jdk-version-for-building-an-applica) but now I get an error. The compilator complains about this line from my build.gradle file `forkOptions.executable = /usr/local/java` – so_user Jan 30 '15 at 15:01
  • Still doesn't compile. Somehow when compiling, the compiler doesn't understand spring annotations. I do have a dependency of it in `gradle.build` – so_user Jan 31 '15 at 16:01

2 Answers2

0

regardless of the comments I still believe that's an PATH problem.

Make sure that everything is ok with your paths and installs

This is the best resource (my opinion) for easy and fast JDK setup on Linux - http://www.wikihow.com/Install-Oracle-Java-JDK-on-Ubuntu-Linux

Check it out.

PS: Another thing that I have in my mind is:

Write a very very very simple HelloWorld.java, just System.out.print("something");

Place it somewhere and navigate with the terminal.

Now try to run it from terminal - java HelloWorld.java if successful, javac HelloWorld

etc.

If this works, it's problem with Gradle. Maybe you or somehow the system has changed the pre-defined JAVA_PATH in Gradle's setup.

Check that out too!

Stuci
  • 571
  • 1
  • 5
  • 13
0

You have three things to check for Windows. On other systems, you will also need to check these things, but the way to do it will be different:

  • If gradle is well accessible. gradle -v will tell you if a version of gradle is found. Otherwise put the gradleX.XX\bin folder in the PATH.
  • If java is well accessible. java -v will tell you if one version of java is found.
  • If JAVA_PATH is properly set. Believe it or not, but java may be accessible from the command line while JAVA_PATH is not properly set. This is because sweet Oracle decided it was cool to copy java executable to Windows\System32 folder. So java is indeed accessible from command line, but Gradle doesn't find it. So, echo %JAVA_HOME%. Go to that folder, and if it doesn't exists (which is most likely why Gradle is complaining about JAVA_HOME), fix the environment variable to point to the actual java installation folder.

And that's it.

Gorpik
  • 10,940
  • 4
  • 36
  • 56
Overdrivr
  • 6,296
  • 5
  • 44
  • 70