Java包装困境

时间:2010-07-20 15:59:48

标签: java import package packing

好的,所以我决定在Java上测试我的测试文件。我决定从我的家用电脑上把文件夹放到我的工作电脑上。问题?当我编译 - 我得到:

C:\Documents and Settings\djasnowski\Desktop\gJson\readGoogle.java:1: package com.google.gson does not exist
import com.google.gson.*;
^
C:\Documents and Settings\djasnowski\Desktop\gJson\readGoogle.java:37: cannot find symbol
symbol  : class Gson
location: class readGoogle
    data = new Gson().fromJson(fileString, Data.class);
               ^
2 errors

这很有趣。我没有动摇我的文件,它在家用电脑上工作正常,现在我明白了。文件设置很好。我在包含Google JSON Java文件的文件夹中有我的com.google.gson,并且我的googleRead.java文件位于正确的目录中...而且我的map1.txt也在那里......很奇怪?我想是的。

这是我的档案:

import com.google.*;

import java.util.*;
import java.io.*;

public class readGoogle {

    public static String MapTitle;
    public static Data data;
    public static Item item;
    public static String dan;
    public static FileReader fr;


        public static void main(String[] args) {
try {
    fr = new FileReader("map1.txt");
}catch(FileNotFoundException fne) {
    fne.printStackTrace();
}
        StringBuffer sb = new StringBuffer();
        char[] b = new char[1000];
        int n = 0;
        try {
        while ((n = fr.read(b)) > 0) {
             sb.append(b, 0, n);
         }
         }catch(IOException rex) {
             rex.printStackTrace();
         }
        String fileString = sb.toString();

    try {
    data = new Gson().fromJson(fileString, Data.class);
    }catch (Exception er) {
        er.printStackTrace();
    }

    System.out.println("Name of map: " + data.getTitle());
    System.out.println("File of map: " + data.getName());
    System.out.println("Current Map: " + data.getCurrentMap());
    System.out.println("Right Map: " + data.getRightMap());

    }

public static class Item {
        public static String name;
        public static int x;
        public int y;

        public static String getName() { return name; }
        public static int getX() { return x; }
        public int getY() { return y; }

        public void setName(String name) { this.name = name; }
        public void setX(int x) { this.x = x; }
        public void setY(int y) { this.y = y; }
    }

      public static class Data {
            private String name;
            private String title;
            private int currentMap;
            private int leftMap;
            private int rightMap;
            private int upMap;
            private int downMap;
            private List<Item> items;
            private int[][] map;

            public String getName() { return name; }
            public String getTitle() { return title; }
            public int getCurrentMap() { return currentMap; }
            public int getUpMap() { return upMap; }
            public int getDownMap() { return downMap; }
            public int getLeftMap() { return leftMap; }
            public int getRightMap() { return rightMap; }
            public List<Item> getItems() { return items; }
            public int[][] getMap() { return map; }

            public void setName(String name) { this.name = name; }
            public void setTitle(String title) { this.title = title; }
            public void setCurrentMap(int currentMap) { this.currentMap = currentMap; }
            public void setItems(List<Item> items) { this.items = items; }
            public void setMap(int[][] map) { this.map = map; }
        }

}

1 个答案:

答案 0 :(得分:1)

第一个错误是:

package com.google.gson does not exist

这意味着在javac的类路径中找不到Google GSON library。听起来你已经将GSON JAR下载到你的工作PC了。如果你还没有,那就先做。

然后,将GSON JAR添加到类路径并重试编译。更改类路径的细节取决于您的工具集 - 与命令行上的javac手动调用相比,IDE的步骤表面上完全不同。如果您不确定如何操作类路径并且使用命令行,请咨询manualGoogle it。我无法从问题中理解你的文件布局,所以如果没有更多的信息,我就不能再具体了。

解决此问题后,您可以考虑使用Apache Maven之类的工具来自动执行依赖关系管理,编译和打包。