找不到 Gson 类型类错误



我有netbeans,我添加了一个带有{"classpath":gson-2.8.0.jar}和{"sources":gson-2.8.0-sources.jar}和{"javadoc":gson-2.8.0-javadoc.jar}的Gson库,如这篇文章中所建议的:

将 Gson 添加到 netbeans Java 项目

现在,当我在下面创建java文件时:

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.*;
class test{
Map<Integer,Set<Integer>> map = new HashMap<Integer,Set<Integer>>(){
{
put(1,new HashSet<Integer>(){{add(3); add(2);}};
}
};
Gson gson = new Gson();
Type typeOfHashMap = new TypeToken<Map<Integer, Set<Integer>>>() { }.getType();
String jsonMap = gson.toJson(map, Map.class);
}

我似乎忘记了包含 main 方法,但你知道代码的含义。当我运行它时,出现以下错误:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Type

根据另一篇文章,我应该从该下载中获得所需的一切,但由于某种原因,Type 类不在文件中。我该如何解决这个问题,我将不胜感激任何帮助。

添加导入语句

import java.lang.reflect.Type;

它是Java语言的一部分,而不是来自外部库。

尝试使用自动导入快捷方式。对于日食(Ctrl + Shift + O(。对于我不确定的 netbeans 可能是(Ctrl + Shift + I(

相关内容

最新更新