Java 打包问题

标签 java import package packing

好的,所以我决定用 Java 进一步处理我的测试文件。我决定从我的家庭 PC 上解压该文件夹并将其带到我的工作 PC 上。问题?当我编译时——我得到:

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

这很有趣。我没有动过我的文件,它在我的家用电脑上运行良好,现在我明白了。文件设置得很好。我的 com.google.gson 位于包含 Google JSON Java 文件的文件夹中,我的 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; }
        }

}

最佳答案

第一个错误是:

package com.google.gson does not exist

这意味着 Google GSON libraryjavac 的类路径中找不到。听起来您已经将 GSON JAR 下载到您的工作 PC 上。如果您还没有这样做,请先这样做。

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

解决此问题后,您可能会考虑使用类似 Apache Maven 的工具自动化依赖管理、编译和打包。

关于Java 打包问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3291939/

相关文章:

java - 我怎样才能更快地减去这些列表?

java - 如何在 xAxis BarChart MPAndroidChart 上设置日期时间的字符串值

r - 在 Linux 中解压然后读取数据,未知格式

arrays - 在 TypeScript 中将 JSON 文件导入为 const

ios - 使用混合的 Swift 和 Objective-C 代码创建框架

java - 确认 oauth2 批准流程后,service/oauth/authorize 中的主体为空(approveR Deny 中的 http post)

android - 能够更改通过Gradle(Android Studio)导入的库的代码吗?

javascript - 使用 Browserify 加载 Node.js 模块

java - java中用户定义包的访问方法

java - 如何从 Perl 调用 Java 程序?