简体   繁体   中英

@com.sun.istack.internal.NotNull annotation

I try to use the @NotNull annotation from package com.sun.istack.internal.

I am using IDE Intellij IDEA Community Edition. when I build a program using IDE no problem. When I try to compile a file from the command line using javac, I get an error "cannot find symbol".

package ibkr;

import com.sun.istack.internal.NotNull;

public class Main {

    public static void main(String[] args) {
        test("Test");
    }

    public static void test(@NotNull String text) {
        System.out.println(text);
    }
}

I don't understand why i can't compile this code using javac and how Intellij IDEA make compilation and run it.

The annotation is an internal class, it's not for public use. The closest alternative is Jetbrains' stuff:

https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html

Even if the class is in the classpath, it doesn't necessarily mean that you can safely refer to that class. The compiler can have some limitations upon accessing some classes/packages.

In most cases, as here, it's obvious whether the package is internal or not: com.sun.istack. internal . Oracle discourages developers from using classes from such packages.

好的,我知道这个问题有点老了,但是如果我的信息是正确的,原因是因为 ct.sym,intellij 使用完整的 rt.jar 进行编译,而 javac 使用不完整的版本

This is an annotation used to identify non-nullable values, also this will let static analyzer have their checks in place. In case you are using IntelliJ you can use its annotation but it would make it very tool-specific, same is the case for eclipse One can also you

https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl

This provides the same non-null annotation but you need to add an extra dependency there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM