java - ERROR CANNOT FIND SYMBOL when compiling a Java file
问题描述
I want to create a RMI application. I set the java path in the System Variables and started with these steps
- javac Hello.java
- javac HelloImpl.java
- javac HelloServer.java
- javac HelloClient.java
- start rmiregistry
- start java -Djava.security.policy=policy Server
- start java -Djava.security.policy=policy Client
But in the second step I have this problem
C:\Users\HP\eclipse\SimpleRMIExample\src>javac Hello.java
C:\Users\HP\eclipse\SimpleRMIExample\src>javac HelloImpl.java
HelloImpl.java:4: error: cannot find symbol
public class HelloImpl extends UnicastRemoteObject implements Hello {
^
symbol: class Hello
1 error
Code
//Hello.java
import java.rmi.*;
public interface Hello extends Remote {
public String getGreeting() throws RemoteException;
}
//HelloImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
// No action needed here.
}
public String getGreeting() throws RemoteException {
return ("Hello there!");
}
}
//HelloServer.java
import java.rmi.*;
public class HelloServer {
private static final String HOST = "localhost";
public static void main(String[] args) throws Exception {
// Create a reference to an implementation object...
HelloImpl temp = new HelloImpl();
// Create the string URL holding the object's name...
String rmiObjectName = "rmi://" + HOST + "/Hello";
// (Could omit host name here, since 'localhost‘ would be assumed by default.)
// 'Bind' the object reference to the name...
Naming.rebind(rmiObjectName, temp);
// Display a message so that we know the process has been completed...
System.out.println("Binding complete...\n");
}
}
//HelloClient.java
import java.rmi.*;
public class HelloClient {
private static final String HOST = "localhost";
public static void main(String[] args) {
try {
// Obtain a reference to the object from the registry and typecast it into the
// appropriate
// type...
Hello greeting = (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
// Use the above reference to invoke the remote object's method...
System.out.println("Message received: " + greeting.getGreeting());
} catch (ConnectException conEx) {
System.out.println("Unable to connect to server!");
System.exit(1);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
}
解决方案
问题终于解决了,步骤如下
- 仅第一次编辑系统环境变量 -> 环境变量 -> 用户变量 -> CLASSPATH(如果找不到则添加)-> path-project-bin-folder (C:\Users\HP\eclipse\SimpleRMIExample\bin)
- 仅第一次重启
- 清洁项目
- 命令:启动 rmiregistry(在项目的 bin 文件夹上)
- 运行项目
推荐阅读
- vim - 如何为使用 :registers 命令打开的缓冲区创建自动命令
- c# - .Net Core 3.1 - 无法跟踪实体类型“城市”的实例 - 使用自定义日期跟踪器时
- reactjs - 如何在反应中仅延迟加载一次
- azure-devops - Azure DevOps 服务器(本地)上传符号 (401) 未经授权
- ignite - Drill 是否可以与 Ignite In-memory DB 一起使用?
- makefile - makefile rules in other rules
- amazon-web-services - 如何构建 AWS CodePipeline 以跨账户将 ECR 容器部署到 ECS Fargate
- palantir-foundry - 是否有用于为 Foundry Magritte / 数据连接代理创建插件的 SDK?
- bash - 使用条件读取文件的每一行以将行存储到 bash 中的另一个文件
- python - 同一 DAG 中相同任务名称但执行日期不同的气流冲突