0

I want to run a Java file that uses another Java class located in the same directory. I have already tried the solution from this link. [Cannot Find Symbol for another class file. I am able to compile both classes successfully now but when I run Example.java, I still encounter the same error.

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

And here is the error.

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>
1
  • 1
    Does this answer your question? How do I run java program with multiple classes from cmd?. You compile all the files by a wildcard, so they are on the classpath. When trying to run java SomeFile.java it compiles and runs only that file. Simply run the Example as class and add other files to classpath Commented Mar 14, 2020 at 2:44

1 Answer 1

0

Because your Country.java source file starts with package org.apache.geode_examples.serialization;, the file must be named org\apache\geode_examples\serialization\Country.java, and you must be in the parent folder when compiling and running.

Run the following 3 commands.

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.