简体   繁体   English

Java软件包无法编译

[英]Java Package Won't Compile

I think my understanding of how to set up a Java package might be missing something because I cannot get my package to compile. 我认为我对如何设置Java软件包的理解可能会有所遗漏,因为我无法编译我的软件包。 I have 3 class files. 我有3个班级文件。 My directory looks like this: ...Documents\\jsjf which contains: ArrayStack.java and StackADT.java. 我的目录如下:... Documents \\ jsjf,其中包含:ArrayStack.java和StackADT.java。 Within the jsjf directory I also have a folder "EmptyCollectionException" which contains EmptyCollectionException.java. 在jsjf目录中,我还有一个文件夹“ EmptyCollectionException”,其中包含EmptyCollectionException.java。 I can compile EmptyCollectionException.java and StackADT.java but when I try to compile ArrayStack.java I get the following error messages (see link): http://i.stack.imgur.com/koJ8P.jpg 我可以编译EmptyCollectionException.java和StackADT.java,但是当我尝试编译ArrayStack.java时,出现以下错误消息(请参阅链接): http ://i.stack.imgur.com/koJ8P.jpg

Here is the top part of each part of my code for each file. 这是每个文件的代码各部分的顶部。 Does anyone know what the problem is here. 有谁知道这是什么问题。 Why is ArrayStack not able to import the package? 为什么ArrayStack无法导入包?

For ArrayStack.java 对于ArrayStack.java

package jsjf;



import jsjf.EmptyCollectionException;
import java.util.Arrays;

public class ArrayStack<T> implements StackADT<T>
{
   private final int DEFAULT_CAPACITY = 100;
   private int top;
   private T[] stack;



   //-----------------------------------------------------------------
   //  Creates an empty stack using the specified capacity.
   // Note top is now initialized at -1 so that when first
   // element is added an top is decremented, top will equal 0
   // corresponding to the array index of the first element.
   //-----------------------------------------------------------------
   public ArrayStack(int initialCapacity)
   {
      top = -1;
      stack = (T[]) (new Object[initialCapacity]);
   }

   //-----------------------------------------------------------------
   //  Creates an empty stack using the default capacity.
   //-----------------------------------------------------------------
   public ArrayStack()
   {
      this(DEFAULT_CAPACITY);
   }


//Rest of code.......

For EmptyCollectionException.java: 对于EmptyCollectionException.java:

package jsjf.exceptions;


public class EmptyCollectionException extends RuntimeException
{
    /**
     * Sets up this exception with an appropriate message.
     * @param collection the name of the collection
     */
    public EmptyCollectionException(String collection)
    {
        super("The " + collection + " is empty.");
    }
}

For StackADT: 对于StackADT:

package jsjf;


public interface StackADT<T>
{
    /**  
     * Adds the specified element to the top of this stack. 
     * @param element element to be pushed onto the stack
     */

    //Rest of Code

Firstly, a package name should not start with an upper case. 首先,程序包名称不应以大写字母开头。 It would have helped you to see that jsjf.EmptyCollectionException does not point to a class in your hierarchy but to a package. 这将有助于您看到jsjf.EmptyCollectionException不在层次结构中指向类,而是指向包。 Therefore, after renaming the package according to the Java naming conventions, the correct import should be : 因此,根据Java命名约定重命名程序包后,正确的导入应为:

import jsjf.emptyCollecionException.EmptyCollectionException

In you exception class, you use another package name ( exceptions ). 在您的异常类中,您使用另一个包名称( exceptions )。 The package name should match the name of the parent directory. 程序包名称应与父目录的名称匹配。 Therefore, I woul rename the directory containing EmptyCollectionException to exception and fix the import in ArrayStack . 因此,我将包含EmptyCollectionException的目录重命名为exception并修复ArrayStack的导入。

Finally, I strongly recommend you to work with an IDE when you will have compiled some classes manually just to know how it works. 最后,强烈建议您在手动编译一些类以了解其工作原理时使用IDE。 An IDE will help you with the imports, compilation, and lots of other things IDE将帮助您进行导入,编译和许多其他操作

Your code is saying you expect EmptyCollectionException to be in the package jsjf , but you say that class exists in a subfolder, which would be deeper in the package hierarchy (and counter to Java naming conventions, where package names usually aren't camel-cased). 您的代码说您希望EmptyCollectionException位于包jsjf ,但您说该类存在于子文件夹中,该子文件夹在包层次结构中更深(并且与Java命名约定相反,在Java命名约定中,包名称通常不使用驼峰式)。

Your EmptyCollectionException class code says it's in the jsjf.exceptions package, meaning (a) it should be in the exceptions folder under the jsjf folder, and your import of EmptyCollectionException should be import jsjf.exceptions. EmptyCollectionException EmptyCollectionException类代码说,这是在jsjf.exceptions包,这意味着(一)它应该是在exceptions下的文件夹jsjf文件夹,您的进口EmptyCollectionException应该是import jsjf.exceptions. EmptyCollectionException import jsjf.exceptions. EmptyCollectionException . import jsjf.exceptions. EmptyCollectionException

You also need to be in a reasonable place when you compile, eg, your documents folder since packages would live "underneath" where you're compiling from. 编译时还需要放在一个合理的位置,例如,documents文件夹,因为程序包将位于“编译源”下面。 Personally, I'd put this into a more reasonable folder location, preferably without spaces, somewhere not under a Windows documents folder. 就个人而言,我会将其放置在Windows文档文件夹下某个更合理的文件夹位置,最好不要使用空格。

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

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