importing NotNull or Nullable and Android Studio won't compile - java

When I add #NotNull or #Nullable annotations to a parameter Android Studio automatically helps me with adding /lib/annotations.jar and importing
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable;
But after this, the project won't compile. If I also remove the annotations but keep the import statements the project still won't compile. But if I remove the import statements for NotNull and Nullable the project compiles fine!
Android Studio gives a generic error:
Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Running gradlew compileDebug from cmd gives a slight hint:
:Bugtester:compileDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> 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.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
So I checked my environment variables and they are set as:
JAVA_HOME=C:\Program Files (x86)\Java\jre7
JDK_HOME=C:\Program Files\Java\jdk1.7.0_21\
Anyone got any idea for this? (I'm new to both java and android programming)

I guess, the right way is using original JetBrains library from MavenCentral repository in your build.gradle dependencies (latest available version in this example):
dependencies {
implementation 'com.intellij:annotations:+#jar'
...
}

You can also use android's own #NonNull & #Nullable:
Add the following to build.gradle:
dependencies {
...
// For #Nullable/#NonNull
compile 'com.android.support:support-annotations:+'
}
Go to File / Setting → Project Settings → Inspections and search for "nullable".
In Constant conditions & exceptions and #NotNull/#Nullable problems, click Configure annotations and select Android's annotations.
You may also want to check out Suggest #Nullable annotations… under Constant conditions & exceptions, or possibly tweak other options.

For using Android support anotation like - #Nullable, #NonNull etc.. In your project must be imported android support annotations library. Just add this line to dependensies in gradle file
dependencies {
compile 'com.android.support:support-annotations:+'
}
And import package to class.
For using #Nullable annotation:
import android.support.annotation.Nullable;
For #NonNull
import android.support.annotation.NonNull;
More info you can find here Android developers

At the moment, there is no NonNull/Nullable annotations in the Android API or in the support library. You also cannot use the IntelliJ one since they are not on the compilation classpath when building with Gradle.
However, you can easily create your own. It's very simple:
#Documented
#Retention(RetentionPolicy.CLASS)
#Target({METHOD,PARAMETER,LOCAL_VARIABLE,FIELD})
public #interface NonNull {
}
Once this is down, you can configure IntelliJ (or Android Studio) to recognize this one (and the matching #Nullable) to be the default annotation used for Null-checks.
To do this, go in the IntelliJ preferences, under Inpections, and then find the #NotNull/#Nullable problems entry under Probable Bugs in the inspection tree.
Select the item, and in the bottom right you'll have a button to "Configure Annotations". This will allow you set your own annotations as the one intelliJ will use for this inspection.

In 2015, you would have used annotations from android.support.annotation package. I am just not sure when they added them as in the docs there isn't that typical sentence "Added in API level X" and I don't feel like reading blogs, etc. >]
import android.support.annotation.NonNull;
...
public void fooFighter(#NonNull Object honeyBunny){
...
}

I am facing that problem for gradle-5.1.1 - Android Studio 3.4 and the error like that - Compilation failed; see the compiler error output for details. Execution failed for task ':app:compileDebugJavaWithJavac'. etc. In this case I use
implementation 'org.jetbrains:annotations:16.0.2'
and above all error will be clear.

Just import androidx.annotation.Nullable for that purpose

The best way is to use the maven dependency
set the repository path, by adding
repositories {
maven {
url "https://repository.jboss.org/nexus/content/repositories/thirdparty-releases"
}
}
add the dependency
dependencies {
compile 'org.jetbrains:annotations:7.0.2'
}
PROFIT!

For Maven add dependency:
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.1</version>
</dependency>

Related

Gradle project import work in IDEA, but don't work in VS Code

I have this gradle java project with this setting.gradle
pluginManagement {
repositories {
maven {
url "${settings.ext.get("artifactory.contextUrl")}/example"
credentials {
username = 'myemail'
password = 'mypassword'
}
}
}
When i import it into IDEA, everything works fine, but i want to import this project into VS Code, but when i try it, gradle build passed just like in IDEA, but it still shows many errors:
error
and java dont see imports imports
obviously it shouldn't be problem with project and config, but with vs code, cause like i said, IDEA import it normally, does any have idea?
I tried turn on nested projects in vs code gradle, but without change, really don't know where could be difference between IDEA and VSCODE gradle.
Install the Gradle for Java extension. Check to see if all dependencies are included.
For more information on using gradle in vscode see here.

SaxonParserTest missing SaxonXQDataSource?

Looking to run the simplest possible example using saxon, and, specifically, Saxon-HE:
thufir#dur:~/NetBeansProjects/helloWorldSaxon$
thufir#dur:~/NetBeansProjects/helloWorldSaxon$ gradle clean run
> Task :compileJava FAILED
/home/thufir/NetBeansProjects/helloWorldSaxon/src/main/java/helloWorldSaxon/App.java:6: error: package com.saxonica.xqj does not exist
import com.saxonica.xqj.SaxonXQDataSource;
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
2 actionable tasks: 2 executed
thufir#dur:~/NetBeansProjects/helloWorldSaxon$
I don't think that's the right import statement, but neither am I sure what would be correct. A closer look at the sample reveals that it's using saxonica and has actually commented out the import:
//import net.sf.saxon.xqj.SaxonXQDataSource;
To re-iterate, would want to stay within Saxon-HE. But how?
From the build file:
compile (group = "net.sf.saxon" , name = "Saxon-HE" , version = "9.9.0-2")
Sticking to Saxon-HE this is the correct import?
Which data source does this ship with?
thufir#dur:~/saxon$
thufir#dur:~/saxon$ jar tf Saxon-HE.jar | grep data
net/sf/saxon/data/
net/sf/saxon/data/analyze-string.xsd
net/sf/saxon/data/casevariants.xml
net/sf/saxon/data/categories.xml
net/sf/saxon/data/chameleon.xsl
net/sf/saxon/data/json.xsd
net/sf/saxon/data/normalizationData.xml
net/sf/saxon/data/override.xsl
net/sf/saxon/data/unicodeBlocks.xml
net/sf/saxon/data/xml-to-json-indent.xsl
net/sf/saxon/data/xml-to-json-pkg.xsl
net/sf/saxon/data/xml-to-json.xsl
net/sf/saxon/data/xpath-functions.scm
net/sf/saxon/data/xpath-functions.xsd
net/sf/saxon/resource/MetadataResource$1.class
net/sf/saxon/resource/MetadataResource.class
thufir#dur:~/saxon$
Do I need more compile dependencies? If so, which ones?
The class SaxonXQDataSource is in package com.saxonica.xqj. If you are using Saxon-HE, then you will need to have the JAR file saxon9-xqj.jar on the classpath.
The reason Saxon's XQJ library is treated differently from the rest of Saxon-HE is that the licensing is different. The XQJ interface is published by Oracle, and Oracle claim that implementations are subject to the Oracle licensing conditions (some people dispute that implementing a published API requires a license, but that's a matter for the lawyers). The Oracle licensing conditions don't conform to the accepted standards for open source licensing (for two reasons: (a) they disallow modifications, and (b) they require implementations to pass all the reference tests). For this reason some software companies who redistribute Saxon-HE prefer to exclude XQJ from the distribution, and to make this easy, we put the XQJ classes in a separate JAR file.

Where/how to get the MSVC dlls Java 1.8.0_144 wants?

I'm experimenting with building my application with Java 1.8.0_144 to workaround an issue that apparently started after that. I run this command to build:
gradle jfxNative -Dorg.gradle.java.home="c:\Program Files\Java\jdk1.8.0_144"
and it stops with this error:
Execution failed for task ':jfxNative'.
> Not found MSVC dlls
Where and how do I get these MSVC dlls?
The full output looks like this:
c:\...\>gradle jfxNative -Dorg.gradle.java.home="c:\Program Files\Java\jdk1.8.0_144"
> Task :jfxNative
The jar lib\lombok-1.16.18.jar has a main class lombok.launch.Main that does not match the declared main tech.dashman.dashman.ConfiguratorApp
The jar lib\jna-4.5.0.jar has a main class com.sun.jna.Native that does not match the declared main tech.dashman.dashman.ConfiguratorApp
The jar lib\javassist-3.22.0-CR2.jar has a main class javassist.CtClass that does not match the declared main tech.dashman.dashman.ConfiguratorApp
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':jfxNative'.
> Not found MSVC dlls
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
5 actionable tasks: 2 executed, 3 up-to-date
My current list of dependencies look like this:
dependencies {
compile "tech.dashman:dashmancommon:1.0.0-SNAPSHOT"
compile "org.projectlombok:lombok:1.16.18"
compile "org.springframework:spring-web:5.0.2.RELEASE"
compile "org.springframework.retry:spring-retry:1.2.2.RELEASE"
compile "com.fasterxml.jackson.core:jackson-databind:2.9.3"
compile "org.kordamp.ikonli:ikonli-javafx:2.1.0"
compile "org.kordamp.ikonli:ikonli-fontawesome5-pack:2.1.1"
compile "net.harawata:appdirs:1.0.1"
compile "io.sentry:sentry:1.6.4"
compile "org.javassist:javassist:3.22.0-CR2"
testCompile "junit:junit:4.12"
}
I tried adding
compile "net.java.dev.jna:jna-platform:4.5.1"
to that list but I'm still getting the same error when trying to build the installer.
Adding it to my buildscript dependencies did not change the error either:
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "de.dynamicfiles.projects.gradle.plugins:javafx-gradle-plugin:8.8.2"
classpath "com.github.ben-manes:gradle-versions-plugin:0.17.0"
classpath "de.dynamicfiles.projects.javafx.bundler:custom-file-extension-windows-bundler:1.0.2-SNAPSHOT"
classpath "net.java.dev.jna:jna-platform:4.5.1"
}
}
Maintainer of the javafx-maven-plugin/Author of the javafx-gradle-plugin here!
Even if this is a very old post, and I did not see this one before (sorry), I might have an answer to this issue. And it happens even today.
While debugging an issue on the plugin (https://github.com/javafx-maven-plugin/javafx-maven-plugin/issues/395) I found that sometimes there are 2 files missing inside the JDK itself.
In case someone has installed any non-Oracle JDK, e.g. OpenLogic, the provided files are incomplete.
The JDK provided by OpenLogic (other others) provides a file called ant-javafx.jar, where all the system native files are contained (can be found inside the installed JDK in the lib-folder). That file is missing the required runtime files, that are required by the launcher.
Here a screenshot of what it looks like with OracleJDK:
Here a screenshot of what it looks with OpenLogic JDK:
As you can see, there are files missing in there, which makes that JDK not equivalent to the OracleJDK sadly.
On maven-plugin level I can not do anything here to fix this, but you might be able to fix that by modifying that JAR-file by adding the missing files. You should be able to find them on your local windows installation at C:\Windows\System32\vcruntime140.dll or C:\Windows\System32\msvcp140.dll. If these files are missing, you just can download a official runtime installer of these Visual C++ Redistributable files here:
for 32-bit: https://aka.ms/vs/16/release/vc_redist.x86.exe
for 64-bit: https://aka.ms/vs/16/release/vc_redist.x64.exe
Another alternative would be to install a different OpenJDK, for example ojdkbuild (https://github.com/ojdkbuild/ojdkbuild). Using chocolatey on Windows just makes this a one-liner choco install ojdkbuild8 and does contain these required files.

Java 9 no class definition exception

So i want to try the http client
package com.company;
import jdk.incubator.http.HttpClient;
public class Main {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
}
}
And my module info looks like this
module com.company {
requires jdk.incubator.httpclient;
}
But i get java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient
And I don't really understand why. My java version is "build 9-ea+ 169" and I use the latest version of IntelliJ idea (2017.1.3). I looked into this answer and it looks like I have to just add requirement into a file, but it doesn't work for some reason.
works fine for me if I use --add-modules jdk.incubator.httpclient as the start-up parameter.
HttpClient client = HttpClient.newHttpClient();
client.executor().execute(() -> System.out.println("Here")); // prints Here
If you say that your module requires it, does not mean it will be included; at it is not included by default.
Either you or IntelliJ must have made a mistake. You are using an incubator module, about which the documentation says:
Incubator modules are part of the JDK run-time image produced by the standard JDK build. However, incubator modules are not resolved by default for applications on the class path. Applications on the class path must use the --add-modules command-line option to request that an incubator module be resolved. Applications developed as modules can specify requires or requires transitive dependences upon an incubator module directly.
I just confirmed that behavior on java-9-ea+169, i.e. I can compile and launch such a module (from the command line) without additional flags.
The fact that you do not get a compile error seems to indicate that IntelliJ correctly includes the module declaration in the compilation. The fact that you get a run-time error and that this answer helped indicates that the JVM does not see the code you launch as a module.
I ran into the same problems
java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient
with java-9-ea+173 and IntelliJ. I followed Eugenes and Nicolais advice to add jdk.incubator.httpclient explicitly to the module path via --add-modules jdk.incubator.httpclient in Run/Debug Configurations (on macOS: Menu Bar -> Run -> Edit Configurations -> Configuration Tab -> VM Options -> --add-modules jdk.incubator.httpclient
After that everything worked fine. Of course you have to add the dependency into the module-info.java like this as said before:
module network {
requires jdk.incubator.httpclient;
}
UPDATE:
With the latest IntelliJ IDEA 2017.2 EAP 172.2953.9 , I don't need to put the --add-modules to the VM Options. It just works out of the box.

"log has private access" error when using Slf4j annotation and Lombok in IntelliJ

I'm using Lombok to add logging to my Java applications. I've been using this for some time, but since I upgraded my IDE from IntelliJ 13 Community edition to 14 Ultimate, I get the following compile errors (using maven):
error: log has private access in SesameServer
This error originates in a subclass of SesameServer:
#Slf4j
public class AnnotatorServices extends SesameServer {
#Override
protected void initialiseRDFStoreManager(Series<Parameter> parameters) throws RepositoryConfigException, RepositoryException {
log.info("<< ------ Annotator Services ------- >>");
}
}
Of course SesameServer also uses the #Slf4j annotation to add logging. The #Slf4j annotation adds the line:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SesameServer.class);
to the class in which it is used (both SesameServer and AnnotatorServices where SesameServer.class is of course replaced by AnnotatorServices.class).
Apparently the compiler thinks I want to use the log variable from SesameServer instead of AnnotatorServices (both classes have variables with the same name).
How can I prevent this problem, i.e. that the SesameServer.log is used instead of AnnotatorServices.log?
NOTE: This still comes up for anyone else googling this error message; so adding my fix hope this helps.
I had a similar issue after reopening a project from pom.xml.
Since my parent class SesameServer was already built in a different module and used a dependency; when I compiled my AnnotatorServices I also saw the error: log has private access in SesameServer
To fix it I just had to turn on Annotation Processing for the module containing AnnotatorServices.
In IntelliJ Community 2017.1.6 the check box was found under:
File -> Settings
"Build, Execution Deployment -> Compiler -> Annotation Processors"
Tick the "Enable annotation processing"
Following steps worked for me
Install Lombok Plugin in Settings=>plugins under Marketplace search for Lombok and install.
Then in IntelliJ Community 2017.1.6 go to File->Invalidate Caches/ Restart and click on both.
Did the trick for me. All the best.
Make sure your subclass is also annotated properly with #Slf4j.
Update your lombok plugin. Sometimes idea does not display the new updates so goto settings => plugins and search for "lombok"
Click "update" and restart idea

Categories

Resources