Unable to find package java.lang

Hi. . . I’m using Mint Linux. A few days ago I replaced my openjdk with Oracle 1.7 and it broke gradle. Now when I try to compile something I get:

Fatal Error: Unable to find package java.lang in classpath or bootclasspath

My JAVA_HOME is set. I tried using the --no-daemon option to try to determine if the JAVA_HOME wasn’t being picked up by new shells but it didn’t seem to have any impact. If I start a new shell, I have no trouble doing basic javac compiles. I do have an alternative listed:

apj@antdesktop ~/documents/work/kp/rtc/arscli $ update-alternatives --display gradle
gradle - auto mode
  link currently points to /opt/gradle/bin/gradle
/opt/gradle/bin/gradle - priority 1
Current 'best' version is '/opt/gradle/bin/gradle'.
apj@antdesktop ~/documents/work/kp/rtc/arscli $ /opt/gradle/bin/gradle -v

------------------------------------------------------------
Gradle 2.5
------------------------------------------------------------

Build time:   2015-07-08 07:38:37 UTC
Build number: none
Revision:     093765bccd3ee722ed5310583e5ed140688a8c2b

Groovy:       2.3.10
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.7.0_79 (Oracle Corporation 24.79-b02)
OS:           Linux 3.11.0-12-generic i386

Is there an easy fix to this?

Perhaps you have JAVA_HOME pointing to a JRE installation instead of a JDK.

No, that’s not it, but thanks for looking. JAVA_HOME is definitely a JDK:

apj@antdesktop ~/documents/work/kp/rtc/arscli $ $JAVA_HOME/bin/javac -version
javac 1.7.0_79

I even went so far as to hardcode the path to rt.jar into the exec statement in the gradle script:

apj@antdesktop ~/documents/work/kp/rtc/arscli $ tail -2 /opt/gradle/bin/gradle
echo exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH:$JAVA_HOME/jre/lib/rt.jar" org.gradle.launcher.GradleMain "$@"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH:$JAVA_HOME/jre/lib/rt.jar" org.gradle.launcher.GradleMain "$@"

apj@antdesktop ~/documents/work/kp/rtc/arscli $ gradle -i --no-daemon -Plibdir=../tpl/lib buildexec /opt/jdk1.7.0_79/bin/java -Dorg.gradle.appname=gradle -classpath /opt/gradle-2.5/lib/gradle-launcher-2.5.jar:/opt/jdk1.7.0_79/jre/lib/rt.jar org.gradle.launcher.GradleMain -i --no-daemon -Plibdir=../tpl/lib build
Starting Build
Settings evaluated using settings file '/home/apj/documents/work/kp/rtc/master/settings.gradle'.
...
All projects evaluated.
Selected primary task 'build' from project :arscli
...
:arscli:compileJava
Executing task ':arscli:compileJava' (up-to-date check took 0.164 secs) due to:
  No history is available.
All input files are considered out-of-date for incremental task ':arscli:compileJava'.
Starting process 'Gradle Compiler Daemon 1'. Working directory: /home/apj/documents/work/kp/rtc/master Command: /opt/jdk1.7.0_79/bin/java -Djava.security.manager=jarjar.org.gradle.process.internal.child.BootstrapSecurityManager -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -cp /home/apj/.gradle/caches/2.5/workerMain/gradle-worker.jar jarjar.org.gradle.process.internal.launcher.GradleWorkerMain 'Gradle Compiler Daemon 1'
Successfully started process 'Gradle Compiler Daemon 1'
Started Gradle compiler daemon (1.088 secs) with fork options DaemonForkOptions{minHeapSize=null, maxHeapSize=null, jvmArgs=[], classpath=[]}.
Executing org.gradle.api.internal.tasks.compile.JdkJavaCompiler@1809bd3 in compiler daemon.
Compiling with JDK Java compiler API.
Fatal Error: Unable to find package java.lang in classpath or bootclasspath
Exception executing org.gradle.api.internal.tasks.compile.JdkJavaCompiler@1809bd3 in compiler daemon: org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details..
:arscli:compileJava FAILED
:arscli:compileJava (Thread[main,5,main]) completed. Took 2.172 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':arscli:compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Total time: 9.464 secs
Stopped 1 compiler daemon(s).

I’m not sure if the daemon it’s referring to (compiler daemon) is different from the daemon that I’m telling it not to start with --no-daemon. It seems like the compiler daemon must be picking up its JAVA_HOME from somewhere else.

Looks like an Ubuntu thing with update-alternatives, update-java-alternative, and an intense desire by Ubuntu to find a non-existent openjdk rather than use the installed Oracle JDK. I haven’t resolved this yet but I see that it’s more of an OS configuration and not gradle. For anyone else reading this in the future, I’ve found these all helpful:

Hi! I am new to gradle and faced exception when TagType.class from tools.jar can not be defined. I am using Debian/linux and had set up via update-alternatives java and javac to oracle jdk1.8. JAVA_HOME also pointed to same jdk. I solved problem by changing desired jdk priority in update-alternatives to highest (2000). I am sure that ‘manual’ mode of alternatives was used in my system, but seems that gradle (i’ve used gradle wrapper) choose jre/jdk which has highest priority and that was open-jdk 1.7 in my case.

I tried several of the suggestions above, but the easiest fix for me was setting JAVA_HOME to point to the installed IBM JDK 1.7 SE edition and adding it to my path variable. For all other projects I use JDK SE 1.8 from Oracle so I do not want to make this change permanently on my system.

set JAVA_HOME="C:\Program Files (x86)\IBM\WebSphere\AppServer\java_1.7.1_64"
set PATH=%JAVA_HOME%\bin;%PATH%
gradle clean war

After upgrading the whole android SDK and ndk and everything, I ran into this. In the end, this is what fixed it for me:

rm ~/.gradle -rf

which caused gradle to be re-downloaded and set up on the next build and it’s working again.