Java 错误:在同一包中找不到具有接口的符号

Java 错误:在同一包中找不到具有接口的符号,java,import,package,Java,Import,Package,你好,我的软件向导网络。我有一个问题,为什么我不能让我的类编译。我不断得到错误:找不到符号。所有这些类都在同一个包blocklaw中。我尝试了导入语句的每一种变体,但似乎无法消除此错误。这里的目录结构是blocklaw-->src-->所有类。 我也尝试更改包声明blocklaw.src,但仍然没有任何帮助。 我会在这里分享我的代码 package blocklaw; import java.security.MessageDigest; /* * returns a

你好,我的软件向导网络。我有一个问题,为什么我不能让我的类编译。我不断得到错误:找不到符号。所有这些类都在同一个包blocklaw中。我尝试了导入语句的每一种变体,但似乎无法消除此错误。这里的目录结构是blocklaw-->src-->所有类。 我也尝试更改包声明blocklaw.src,但仍然没有任何帮助。 我会在这里分享我的代码

 package blocklaw;

    import java.security.MessageDigest;
    /*
    * returns a string value of the true fact
    */
    public class TrueFact implements Hashable {
      private String trueFact;
      private String hashFact;
      private String source;
      private String hashSource;
      private int bytesLength;
      private String hashedBytesLength;
      private String hashedHash;

      public TrueFact(String trueFact, String source){
        this.trueFact = trueFact;
        this.source = new Source(source).getSource();
        byte[] bytes = trueFact.getBytes("UTF-8");
        this.bytesLength = bytes.length;
        setHashCode();
      }

      public String getTrueFact(){
         return this.trueFact;
      }

      public String getHashFact(){
        return this.hashFact;
      }

      public String getSource(){
        return this.source;
      }

      public int getbytesLength(){
        return this.bytesLength;
      }

      public String getHashedBytesLength(){
        return this.hashedBytesLength;
      }

      public void setHashCode(){
        this.hashFact = hash(this.trueFact);
        this.hashSource = hash(this.source);
        this.hashedBytesLength = hash(String.valueOf(this.bytesLength));
        this.hashedHash = hash(this.hashFact+this.hashSource+this.hashedBytesLength);
      }

      public String getHashSource(){
        return this.hashSource;
      }

      public String hash(String input){
        try {
           MessageDigest digest = MessageDigest.getInstance("SHA-256");

           //Applies sha256 to our input,
           byte[] hash = digest.digest(input.getBytes("UTF-8"));

           StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
           for (int i = 0; i < hash.length; i++) {
              String hex = Integer.toHexString(0xff & hash[i]);
              if(hex.length() == 1) hexString.append('0');
              hexString.append(hex);
           }
           return hexString.toString();
        }
        catch(Exception e) {
           throw new RuntimeException(e);
        }
      }
     }

如前所述,这些是更正答案所需遵循的步骤

Compile Source.java
And the String input parameter to the hash function in Hashable.java
Compile Hashable.java
Now go one folder level up.
Add necessary try catch blocks in TrueFact.java
Build blocklaw/TrueFact.java

这将编译TrueFact.java。

在一个接口中,所有方法本质上都是公共的和抽象的。所以不需要在公共字符串hash()中显式地提到public关键字;方法 相反,您可以只说String hash();
还要确保您的哈希类位于同一目录和同一包中。

您知道该错误是针对哪个类的吗。那个班是私人班吗?是这个班。我正在尝试编译以便测试这个类。接口是公共的,有一个公共类hash()。您的代码应该编译,如果公共类(或受保护的,或包私有的)与您的类在同一个包中,则无需专门导入。但是您到底是如何编译这段代码的呢?看起来您使用的是
javac
?确切地说,您使用的是什么命令?上面代码中的哪一行导致了编译失败?Daniele…好问题,我正在使用$:javac TrueFact.javaOK我删除了src文件夹并将所有文件放在blocklaw目录下,首先编译了Hashable接口,但仍然得到相同的错误。也就是说,找不到符号。是的,Hashable在文件夹blocklaw中,我在原始post中添加了接口。您可以将Hashable接口的代码与包一起发布吗?另外,请确认在使用javac编译后,blocksaw包中是否存在用于Hashable的.class文件。是的,先生,我在-->中添加了它在包blocklaw中;我正在使用javac TrueFact.java来编译.Sujay,我很乐意,但我不知道怎么做??原谅我。告诉我如何“接受”一个答案,我会帮你完成的。
Compile Source.java
And the String input parameter to the hash function in Hashable.java
Compile Hashable.java
Now go one folder level up.
Add necessary try catch blocks in TrueFact.java
Build blocklaw/TrueFact.java