简体   繁体   English

编译 Java 文件时出现错误 CANNOT FIND SYMBOL

[英]ERROR CANNOT FIND SYMBOL when compiling a Java file

I want to create a RMI application.我想创建一个 RMI 应用程序。 I set the java path in the System Variables and started with these steps我在系统变量中设置了 java 路径并从这些步骤开始

  1. javac Hello.java javac Hello.java
  2. javac HelloImpl.java javac HelloImpl.java
  3. javac HelloServer.java javac HelloServer.java
  4. javac HelloClient.java javac HelloClient.java
  5. start rmiregistry启动 rmiregistry
  6. start java -Djava.security.policy=policy Server启动 java -Djava.security.policy=policy 服务器
  7. start java -Djava.security.policy=policy Client启动 java -Djava.security.policy=policy 客户端

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


Problem finally solved here are the steps问题终于解决了,步骤如下

  1. Edit the System Environment Variables -> Environment Variables -> User variables -> CLASSPATH (add if not found) -> path-project-bin-folder (C:\Users\HP\eclipse\SimpleRMIExample\bin) for 1st time only仅第一次编辑系统环境变量 -> 环境变量 -> 用户变量 -> CLASSPATH(如果找不到则添加)-> path-project-bin-folder (C:\Users\HP\eclipse\SimpleRMIExample\bin)
  2. Reboot for 1st time only仅第一次重启
  3. Clean Project清洁项目
  4. Command: start rmiregistry (on bin folder of your project)命令:启动 rmiregistry(在项目的 bin 文件夹上)
  5. Run project运行项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM