简体   繁体   中英

Gson Type class not found Error

I have netbeans and I added a Gson library with {"classpath" : gson-2.8.0.jar} and {"sources" : gson-2.8.0-sources.jar} and {"javadoc" : gson-2.8.0-javadoc.jar}, as suggested in this post:

Adding Gson to netbeans java project

Now when I create java file below:

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);

}

I seem to have forgetten to include main method but you know what the code means. When I run this I get the following error:

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

According to the other post I shoudl have gotten everything I need from that download, but for some reason the Type class wasnt in the files. How do I fix this please I'd appreciate any help.

Add import statement

import java.lang.reflect.Type;

Its part of java language, not from external libraries.

Try to use automatic import shortcuts. For eclipse (Ctrl + Shift + O) . For netbeans I am not sure may be (Ctrl + Shift + I)

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