开发者社区> 问答> 正文

即使成功编译后,运行包含另一个Java类的文件时,“找不到符号错误”

我想运行一个Java文件,该文件使用位于同一目录中的另一个Java类。我已经尝试过此链接中的解决方案。 找不到另一个类文件的符号。我现在可以成功编译两个类,但是当我运行Example.java时,仍然遇到相同的错误。

Example.java

package org.apache.geode_examples.serialization;

import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientCacheFactory;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.pdx.ReflectionBasedAutoSerializer;

import java.util.HashSet;
import java.util.Set;

public class Example {
  public static final String ARENDELLE = "Arendelle";
  public static final String BORDURIA = "Borduria";
  public static final String CASCADIA = "Cascadia";
  public static final String ELBONIA = "Elbonia";
  public static final String FLORIN = "Florin";
  public static final String GRAUSTARK = "Graustark";
  public static final String LATVERIA = "Latveria";
  public static final String MARKOVIA = "Markovia";
  public static final String PARADOR = "Parador";
  public static final String SIERRA_GORDO = "Sierra Gordo";
  final Region<String, Country> region;

  public Example(Region<String, Country> region) {
    this.region = region;
  }

  public static void main(String[] args) {
    // connect to the locator using default port 10334
    ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
        .set("log-level", "WARN")
        .setPdxSerializer(
            new ReflectionBasedAutoSerializer("org.apache.geode_examples.serialization.Country"))
        .create();

    // create a local region that matches the server region
    Region<String, Country> region =
        cache.<String, Country>createClientRegionFactory(ClientRegionShortcut.PROXY)
            .create("example-region");

    Example example = new Example(region);
    example.insertValues();
    example.printValues(example.getKeys());

    cache.close();
  }

  Country create(String name) {
    return create(name, name + " City");
  }

  Country create(String name, String capitol) {
    return create(name, capitol, "");
  }

  Country create(String name, String capitol, String language) {
    return create(name, capitol, language, "", 0);
  }

  Country create(String name, String capitol, String language, String currency, int population) {
    return new Country(name, capitol, language, currency, population);
  }

  Set<String> getKeys() {
    return new HashSet<>(region.keySetOnServer());
  }

  void insertValues() {
    insertValue(create(ARENDELLE, "Arendelle City", "Arendellii", "Arendelle Krona", 76573));
    insertValue(create(BORDURIA, "Szohôd", "Bordurian", "Bordurian Dinar", 1000000));
    insertValue(create(CASCADIA, "Portland", "Pacific Northwest English", "United States Dollar",
        16029520));
    insertValue(create(ELBONIA));
    insertValue(create(FLORIN));
    insertValue(create(GRAUSTARK, "Edelweiss"));
    insertValue(create(LATVERIA, "Doomstadt", "Latverian", "Latverian Franc", 500000));
    insertValue(create(MARKOVIA, "Markovburg", "German"));
    insertValue(create(PARADOR));
    insertValue(create(SIERRA_GORDO, "Rio Lindo", "Spanish"));
  }

  void insertValue(Country country) {
    region.put(country.getName(), country);
  }

  void printValues(Set<String> keys) {
    for (String key : keys) {
      Country country = region.get(key);
      System.out.println(key + ": " + country);
    }
  }
}

Country.java

package org.apache.geode_examples.serialization;

/**
 * <strong>Explicitly</strong> not serializable by java.io.Serializable,
 * org.apache.geode.DataSerializable, or org.apache.geode.pdx.PdxSerializable.
 */
public class Country {
  protected String name;
  protected String capitol;
  protected String language;
  protected String currency;
  protected int population;

  public Country() {
    this("", "", "", "", 0);
  }

  protected Country(String name, String capitol, String language, String currency, int population) {
    this.name = name;
    this.capitol = capitol;
    this.language = language;
    this.currency = currency;
    this.population = population;
  }

  public String getName() {
    return name;
  }

  public String getCapitol() {
    return capitol;
  }

  public void setCapitol(String capitol) {
    this.capitol = capitol;
  }

  public String getLanguage() {
    return language;
  }

  public void setLanguage(String language) {
    this.language = language;
  }

  public String getCurrency() {
    return currency;
  }

  public void setCurrency(String currency) {
    this.currency = currency;
  }

  public int getPopulation() {
    return population;
  }

  public void setPopulation(int population) {
    this.population = population;
  }

  public String toString() {
    StringBuilder builder = new StringBuilder();
    if (name != null && !name.isEmpty()) {
      builder.append(name);
      builder.append(" (");

      if (capitol != null && !capitol.isEmpty()) {
        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 1)) {
          builder.append(", ");
        }
        builder.append("Capitol: ");
        builder.append(capitol);
      }

      if (language != null && !language.isEmpty()) {
        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 1)) {
          builder.append(", ");
        }
        builder.append("Language: ");
        builder.append(language);
      }

      if (currency != null && !currency.isEmpty()) {
        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 1)) {
          builder.append(", ");
        }
        builder.append("Currency: ");
        builder.append(currency);
      }

      if (0 < population) {
        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 1)) {
          builder.append(", ");
        }
        builder.append("Population: ");
        builder.append(population);
      }

      builder.append(")");
    }
    return builder.toString();
  }
}

这是错误。

C:\Users\Milin\iCloudDrive\ENSE 885AO - Readings in Cloud Computing\geode-examples\serialization\src\main\java\org\apache\geode_examples\serialization>javac *.java

C:\Users\Milin\iCloudDrive\ENSE 885AO - Readings in Cloud Computing\geode-examples\serialization\src\main\java\org\apache\geode_examples\serialization>java Example.java
Example.java:37: error: cannot find symbol
  final Region<String, Country> region;
                       ^
  symbol:   class Country
  location: class Example
Example.java:39: error: cannot find symbol
  public Example(Region<String, Country> region) {
                                ^
  symbol:   class Country
  location: class Example
Example.java:63: error: cannot find symbol
  Country create(String name) {
  ^
  symbol:   class Country
  location: class Example
Example.java:67: error: cannot find symbol
  Country create(String name, String capitol) {
  ^
  symbol:   class Country
  location: class Example
Example.java:71: error: cannot find symbol
  Country create(String name, String capitol, String language) {
  ^
  symbol:   class Country
  location: class Example
Example.java:75: error: cannot find symbol
  Country create(String name, String capitol, String language, String currency, int population) {
  ^
  symbol:   class Country
  location: class Example
Example.java:97: error: cannot find symbol
  void insertValue(Country country) {
                   ^
  symbol:   class Country
  location: class Example
Example.java:52: error: cannot find symbol
    Region<String, Country> region =
                   ^
  symbol:   class Country
  location: class Example
Example.java:53: error: cannot find symbol
        cache.<String, Country>createClientRegionFactory(ClientRegionShortcut.PROXY)
                       ^
  symbol:   class Country
  location: class Example
Example.java:76: error: cannot find symbol
    return new Country(name, capitol, language, currency, population);
               ^
  symbol:   class Country
  location: class Example
Example.java:103: error: cannot find symbol
      Country country = region.get(key);
      ^
  symbol:   class Country
  location: class Example
11 errors
error: compilation failed

C:\Users\Milin\iCloudDrive\ENSE 885AO - Readings in Cloud Computing\geode-examples\serialization\src\main\java\org\apache\geode_examples\serialization>

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 14:27:41 904 0
1 条回答
写回答
取消 提交回答
  • 因为您的Country.java源文件以开头package org.apache.geode_examples.serialization;,所以该文件必须命名为org\apache\geode_examples\serialization\Country.java,并且在编译和运行时必须位于父文件夹中。

    运行以下3个命令。

    cd "C:\Users\Milin\iCloudDrive\ENSE 885AO - Readings in Cloud Computing\geode-examples\serialization\src\main\java"
    
    javac org\apache\geode_examples\serialization\*.java
    
    java org.apache.geode_examples.serialization.Example
    

    回答来源:Stack Overflow

    2020-03-26 14:28:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载