【问题标题】:Java Compile Error: Cannot Find Symbol (EraserThread)Java 编译错误:找不到符号 (EraserThread)
【发布时间】:2013-12-06 09:07:09
【问题描述】:

Java 新手,使用模板。知道有什么问题吗?我正在使用 Netbeans IDE 并将 java 文件上传到 linux 机器,我正在那里编译。

PasswordField.java:42: cannot find symbol
symbol  : class EraserThread
location: class PasswordField
  EraserThread et = new EraserThread(prompt);
  ^
PasswordField.java:42: cannot find symbol
symbol  : class EraserThread
location: class PasswordField
  EraserThread et = new EraserThread(prompt);
                        ^
2 errors

public class PasswordField { 
public static String readPassword (String prompt) {
  EraserThread et = new EraserThread(prompt);
  Thread mask = new Thread(et);
  mask.start();

  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String password = "";

  try {
     password = in.readLine();
  } catch (IOException ioe) {
    ioe.printStackTrace();
  }
  // stop masking
  et.stopMasking();
  // return the password entered by the user
  return password;
} 
}

【问题讨论】:

  • 那么EraserThread 类在哪里声明?
  • 导入 EraserThread 类..
  • 对不起,我对此很陌生,但这是在网上找到的模板,我知道错误是找不到课程...我只是想创建一个空的 EraserThread 类?模板就是这样给出的。
  • 如果您可以依赖 Java 6 或更高版本,请不要使用此 EraserThread kludge,如果您在输入密码时不小心让程序运行(而不键入任何内容),它将占用您所有的 CPU。请改用docs.oracle.com/javase/6/docs/api/java/io/…

标签: java compilation symbols


【解决方案1】:

您需要首先创建(或复制)EraserThread 类。这是代码。您需要转到您的项目资源管理器并在您创建上述类的同一个包中,创建一个名为“EraserThread”的新类。然后将其粘贴到类中:

import java.awt.event.KeyListener;
import java.io.*;

public class EraserThread implements Runnable {
    private boolean stop;

    /**
    *@param The prompt displayed to the user
    */
    public EraserThread(String prompt) {
        System.out.print(prompt);
    }

    /**
    * Begin masking...display asterisks (*)
    */
    public void run () {
        stop = true;
        while (stop) {
            System.out.print("/010*");
            try {
                Thread.currentThread().sleep(1);
            } catch(InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }

    /**
    * Instruct the thread to stop masking
    */
    public void stopMasking() {
        this.stop = false;
    }
}

创建类后,您应该能够运行您的代码。

代码来源:http://code.google.com/p/testsome/source/browse/trunk/test/src/EraserThread.java?r=3

编辑: 我不确定这个类是否已经存在于其他地方。您可能可以在不创建它的情况下导入它,但我不确定。自己创建这个类应该没问题,不用通过代码导入。

【讨论】:

  • 不,该类不存在,非常感谢! java新手,只能使用PHP..谢谢!!!!
  • 嗨,我刚刚开始添加课程。虽然我没有收到错误,但我确实收到了这个......
  • 010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/ 010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/0010*/010*/010*/010* /010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010 */010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/010*/ 010*/010*/010*/010*
  • 我不太确定 System.out.println("/010*") 的用途是什么,我希望其他人能够了解这一点。我自己没有使用过这个类,只是遇到过一两次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 2013-10-04
  • 2012-08-27
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多