views:

53

answers:

1

OK so I decided to work somemore on my test files in Java.. I decided to .rar up the folder from my Home PC and bring it to my Work PC. The problem? When I compile -- I get:

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

That is funny. I left my files untouched and it worked fine on my Home PC and now I get this. The files are set up nicely. I have my com.google.gson in the folder containing the Google JSON Java files and I have my googleRead.java file in the correct directory... and my map1.txt is there too... weird? I think so.

Here is my file:

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  A: 

The first error was:

package com.google.gson does not exist

This means the Google GSON library was not found on javac's classpath. It sounds like you've already downloaded the GSON JAR to your work PC. Do that first if you haven't already.

Then, add the GSON JAR to the classpath and retry the compile. The specifics of changing your classpath depend your toolset -- the steps are superficially quite different for an IDE compared to hand-invocation of javac on the command-line. If you're not sure how to manipulate the classpath and you are using the command-line, then consult the manual or Google it. I couldn't understand your file layout from the question, so I can't be any more specific without more information.

Once you solve this problem, you might consider using a tool like Apache Maven to automate dependency management, compilation, and packaging.

Dan LaRocque
That's weird. I didn't think I need to set the class path. I don't think I did it on my last one.... Maybe it is because at home I use Vista-64 and here I use XP-32...
Dan
@Dan not related to the OS. Java works the same across all Windows/Linux/Mac OSs for CLASSPATH
Romain Hippeau
Romain is right. If you are doing anything nontrivial (you are), the classpath will matter. Also, the classpath of the SDK tools (java, javac, etc) can be modified at least two ways: environment variable and the `-cp` or `-classpath` command-line option. Maybe the classpath was set as an environment variable on your home box, and you didn't notice it that way.
Dan LaRocque