gpt4 book ai didi

java - 即使在成功编译后运行包含另一个 Java 类的文件时也会出现 "Cannot find symbol error"

转载 作者:行者123 更新时间:2023-12-01 17:58:12 26 4
gpt4 key购买 nike

我想运行一个使用同一目录中另一个 Java 类的 Java 文件。我已经尝试过此链接中的解决方案。[Cannot Find Symbol for another class file 。我现在可以成功编译这两个类,但是当我运行Example.java时,我仍然遇到相同的错误。

示例.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);
}
}
}

国家.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>

最佳答案

由于您的 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

关于java - 即使在成功编译后运行包含另一个 Java 类的文件时也会出现 "Cannot find symbol error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60679262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com