IntelliJ fails to import gradle project - gradle

IntelliJ fails to import my Gradle project.
The build window shows that the demon gets started on the right side, and on the left side it just displays
<project-name> failed <timestamp>
And indeed no dependencies get resolved, source folders don't get set.
How do I debug/fix this?

The first step should be to build the project on the command line and make sure it works.
Then locate the log file of IntelliJ: Help -> Show Log in Finder does it for me on Mac OS, I guess there is something similar on other platforms.
My log showed:
2021-03-16 11:08:31,370 [222013096] INFO - .project.GradleProjectResolver - Gradle project resolve error
org.gradle.tooling.GradleConnectionException: Could not run phased build action using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-6.8.3-bin.zip'.
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:55)
at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:29)
at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:43)
at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:69)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.gradle.launcher.daemon.client.DaemonConnectionException: The newly created daemon process has a different context than expected.
It won't be possible to reconnect to this daemon. Context mismatch:
Java home is different.
Wanted: DefaultDaemonContext[uid=null,javaHome=/Users/jschauder/.sdkman/candidates/java/current,daemonRegistryDir=/Users/jschauder/.gradle/daemon,pid=981,idleTimeout=null,priority=NORMAL,daemonOpts=-Dfile.encoding=UTF-8,-Duser.country=GB,-Duser.language=en,-Duser.variant]
Actual: DefaultDaemonContext[uid=ad74fb8f-4b76-4109-9780-e222fddedd94,javaHome=/Users/jschauder/.sdkman/candidates/java/8.0.282-zulu/zulu-8.jdk/Contents/Home,daemonRegistryDir=/Users/jschauder/.gradle/daemon,pid=89910,idleTimeout=10800000,priority=NORMAL,daemonOpts=-Dfile.encoding=UTF-8,-Duser.country=GB,-Duser.language=en,-Duser.variant]
The important part is:
Wanted: DefaultDaemonContext[uid=null,javaHome=/Users/jschauder/.sdkman/candidates/java/current,daemonRegistryDir=/Users/jschauder/.gradle/daemon,pid=981,idleTimeout=null,priority=NORMAL,daemonOpts=-Dfile.encoding=UTF-8,-Duser.country=GB,-Duser.language=en,-Duser.variant]
Actual: DefaultDaemonContext[uid=ad74fb8f-4b76-4109-9780-e222fddedd94,javaHome=/Users/jschauder/.sdkman/candidates/java/8.0.282-zulu/zulu-8.jdk/Contents/Home,daemonRegistryDir=/Users/jschauder/.gradle/daemon,pid=89910,idleTimeout=10800000,priority=NORMAL,daemonOpts=-Dfile.encoding=UTF-8,-Duser.country=GB,-Duser.language=en,-Duser.variant]
I.e. the gradle demon runs with a different JDK than what IntelliJ expects.
To fix this you
go into the preferences of Intellij,
search for "gradle"
And locate the "Gradle JVM" setting and use the JDK path from the "Actual" line in the logs. In my case /Users/jschauder/.sdkman/candidates/java/8.0.282-zulu/zulu-8.jdk/Contents/Home
After that importing the project worked just fine.

Related

Can't upgrade to gradle 6.6 because of "prefix string too short"

I'm trying to upgrade an existing project from gradle 6.5.1 to 6.6.
But when I build, it fails with an error:
Successfully started process 'Gradle build daemon'
An attempt to start the daemon took 1.116 secs.
The client will now receive all logging from the daemon (pid: 122). The daemon log file: /home/****/.gradle/daemon/6.6/daemon-122.out.log
Starting build in new daemon [memory: 519.0 MB]
Using 1 worker leases.
Starting Build
Compiling initialization script '/home/****/agent/workspace/Enrich_build_PR-41#tmp/artifactory/init-artifactory449129576487257952gradle' using SubsetScriptTransformer.
FAILURE: Build failed with an exception.
* What went wrong:
Failed to create Jar file /home/****/.gradle/caches/jars-8/8535adf040d7c5fdb7fd6bc28bb0ef3f/ok.
> Prefix string too short
Running with --debug, the only thing that looks relevant is:
020-10-05T16:38:54.631+0000 [DEBUG] [org.gradle.internal.component.model.LoggingAttributeMatchingExplanationBuilder] Selected match ok from candidates [ok] for {org.gradle.dependency.bundling=external, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}
Which seems like an internal gradle issue. How can I fix or avoid this error?
(I also tried 6.6.1 and have the same problem)
It happens when I run "./gradlew help" as well, so it shouldn't be related to my build config.
The stacktrace looks like:
* Exception is:
org.gradle.api.GradleException: Failed to create Jar file /home/****/.gradle/caches/jars-8/8535adf040d7c5fdb7fd6bc28bb0ef3f/ok.
at org.gradle.internal.classpath.ClasspathBuilder.jar(ClasspathBuilder.java:47)
at org.gradle.internal.classpath.InstrumentingClasspathFileTransformer.transform(InstrumentingClasspathFileTransformer.java:76)
at org.gradle.internal.classpath.InstrumentingClasspathFileTransformer.transform(InstrumentingClasspathFileTransformer.java:70)
at org.gradle.internal.classpath.DefaultCachedClasspathTransformer$TransformFile.lambda$schedule$0(DefaultCachedClasspathTransformer.java:261)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
Caused by: java.lang.IllegalArgumentException: Prefix string too short
at org.gradle.internal.classpath.ClasspathBuilder.buildJar(ClasspathBuilder.java:53)
at org.gradle.internal.classpath.ClasspathBuilder.jar(ClasspathBuilder.java:45)
... 6 more
Your issue seems to be related to artifactory plugin
Check https://github.com/jfrog/jenkins-artifactory-plugin/issues/349
Quick Fix
Recommend to downgrade to Gradle 6.5 to resolve the issue.
Further Details / Possible Cause
While I don't know the root cause of why this happens, I get the same 'Prefix string too short' error, in Gradle 6.6.1.
And in particular, this is my exact error message, from a Mac:
Failed to create Jar file /Users/****/.gradle/caches/jars-8/8535adf040d7c5fdb7fd6bc28bb0ef3f/ok.
Since the 'prefix' error came from Java, I looked at my Java install thinking that might be the problem. In my case Java was many versions behind, so I upgraded to 8u261 however that didn't seem to change anything.
For how it starts in our case:
User kicks off Jenkins job for Gradle
Jenkins job fails specifically for being unable to create the file in the cache directory. And the number actually is the same each time for the cache too; it's always '.gradle/caches/jars-8/8535adf040d7c5fdb7fd6bc28bb0ef3f/ok' and not a different number. So it's almost like that is statically set from somewhere.
So focusing on that error, I came across this similar-ish prefix error, for a Java issue. While this doesn't fully answer the issue we see, it does help explain why it could be a 'legitimate' error as it thinks the file name is less than 3 characters.
https://stackoverflow.com/a/48806741/14403001
So it feels like it's interpreting the end of that string as being the file name. Namely it might think that ok at the end of the path is actually a filename, and being less than 3 characters it sends the 'Prefix string too short' error. Keep in mind this is speculation and I don't know if this is actually the cause or not.
We happened to have another Mac that was running Gradle 6.5 that was working fine with the exact same Jenkins job. So we downgraded from Gradle 6.6.1 to 6.5 to match the other and are back working like before.
Since we used Mac, Brew didn't actually have an older version to roll back to. So I was forced to uninstall gradle via brew, then follow the steps below to install and point to an older version of Gradle.
Hoping it's easier for you via yum/apt to downgrade than it was for myself.
https://gradle.org/install/#manually

Gradle build failing after update

Getting exception while gradle/gradlew build. Build without test cases is successful. This happening when I updated the gradle to 6.0.1
Works fine for gradle 5.6/6.2 version.
Could not write standard input to Gradle Test Executor 2.
java.io.IOException: The pipe is being closed
at java.base/java.io.FileOutputStream.writeBytes(Native Method)
at java.base/java.io.FileOutputStream.write(FileOutputStream.java:348)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.forwardContent(ExecOutputHandleRunner.java:68)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.run(ExecOutputHandleRunner.java:53)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
at java.base/java.lang.Thread.run(Thread.java:835)
Error: Could not find or load main class worker.org.gradle.process.internal.worker.GradleWorkerMain
Caused by: java.lang.ClassNotFoundException: worker.org.gradle.process.internal.worker.GradleWorkerMain
FAILURE: Build failed with an exception.
I just had this happen, and found that stopping the gradle daemon resolved it for me:
gradle --stop
or
./gradlew --stop
depending on your setup (the daemon is relaunched when you attempt to build again).
I encountered this error and the one method I found was dump the whole $HOME/.gradle cache and then rebuild the project.
Starting/Stopping IntellijIDEA and/or rebuilding did not help.
You know you have jumped the complexity shark somewhere when running lowly test cases starts to fail for obscure reasons.
Granted, this happened after a machine crash. But still..
If its a windows system, then try deleting the .gradle folder from the user directory and try to build the gradle. This should solve the issue

Gradle / IntelliJ miscommunication causing "Unable to start daemon process"

My IntelliJ Idea projects have been working fine until I cloned a new project from our repo and installed Gradle.
This new project runs, but running the debugger causes the following error:
I've completed the steps recommended in this similar SO question. I have also read the Gradle Daemon guide.
Some helpful information:
-Gradle does respond to command line prompts, including --profile and --status:
-The project builds and executes; the problem is only the debugger. Is this a mapping issue between IntelliJ and Gradle?
-In the Settings menu, Gradle JVM: is set to "Use Project JDK (Java version "1.8.0_211") and this program is written in Java 8. Delegate settings set to: Build and run using intelliJ IDEA. Run tests using IntelliJ IDEA. Should these change to Gradle?
-I took the advice of one of the respondents and had one of our IT guys check out my ports and firewall settings. He was unable to solve the problem and said the issue is with Gradle.
So far, nothing has worked. What should I do?
I solved this problem after some tinkering:
In (IntelliJ) File > Settings > Build, Execution, Deployment > Gradle, I changed "Use gradle 'wrapper' task configuration" to "Use local gradle distribution."
Also, notice in this screenshot that "Gradle VM options" has a custom value; this was suggested as a solution in another post. This relates to memory management, as some people speculated that the daemon was crashing because of a RAM issue. My solution appears to work regardless of whether I alter that value.

macOS: how to install JavaFX properly to run mqtt-spy?

I'm trying to run mqtt-spy-1.0.0.jar on my macOS 10.14 system but it won't start, returning the following error message:
According to the developers, this problem is caused if JavaFX is missing on the system.
The latest version of the Oracle JDK is installed on my system as can be seen below, however, I'm aware that Oracle has excluded JavaFX from the JDK in v11.
So I downloaded JavaFX from GluonHQ and followed their instructions on how to get started.
Despite having both required variables set correctly in ~/.bash_profile, mqtt-spy-1.0.0.jar is still returning the error message shown on the first screenshot ...
What else do I need to do or what do I need to do differently to run mqtt-spy?
There is already an issue filed about this, but not a solution.
I haven't really tried to get it fully working, but these are the required steps to run a jar on Java 11 that requires JavaFX 11, but doesn't bundle it:
Go to OpenJFX docs and read about how to get started with JavaFX 11.
Download JavaFX 11 for your platform from here. Unzip it
Providing that you have Java 11 installed, and set as JAVA_HOME:
With mqtt-spy-1.0.0.jar (as latest release), you can run:
java --module-path /path-to/javafx-sdk-11.0.1/lib \
--add-modules javafx.controls,javafx.fxml -jar mqtt-spy-1.0.0.jar
After you run this, you will get this exception:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
Since Java 9, JAXB is not part of the JDK either. So you can add try to download the dependency from here) and add it to the classpath. But this will take some iterations (there are a few other required jars more, see this).
So why don't you use the latest snapshot available, that includes dependencies: mqtt-spy-1.0.1-beta-b18-jar-with-dependencies.jar.
With this:
java --module-path /path-to/javafx-sdk-11.0.1/lib \
--add-modules javafx.controls,javafx.fxml -jar mqtt-spy-1.0.1-beta-b18-jar-with-dependencies.jar
I get:
Warning: But this doesn't mean that the app will fully work. Given that it is a Java 8 app, there are things that have changed in JavaFX 11, mainly related to the control skins. If the app was using private API (com.sun.javafx....), that won't work now, because either it has been moved to public packages, or because it is not accessible by the modules. For the latter you can use --add-opens, but for the former there is no solution other than update the app dependencies to Java 9+.

Can't generate the APK

The App works perfectly in debug variant but when I change it to release variant the build fails and it gives me this error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details, and also on my java classes (setContentView(R.layout.activity_main)) the R tells me it can't resolve symbol 'R'.
This only happens when I change the build variant to release to finish the app.
Thank you in advance!

Resources