简体   繁体   English

构建 Java 源文件和/或声明/导入包以确保无错误编译的规则是什么?

[英]What are the rules for structuring Java source files, and/or declaring/importing packages to ensure error-free compilation?

I have several files kept in a package, customQ .我有几个文件保存在 package, customQ中。 This package is imported into a test client called QWithExceptions .这个 package 被导入一个名为QWithExceptions的测试客户端。 The source tree is arranged as follows:源码树排列如下:

/tryThis09_01/+
              |
              -QWithExceptions.java
              |
              /customQ/+
                       |
                       -circularQ.java
                       -staticQ.java
                       -emptyQException.java
                       -fullQException.java

The test client compiles without error when I cd to tryThis09_01/ and run $javac QWithExceptions.java .当我cdtryThis09_01/并运行$javac QWithExceptions.java时,测试客户端编译没有错误。

However, when I decided to experiment a little I find that my intuition regarding packages and their structure is completely off.然而,当我决定尝试一下时,我发现我对包及其结构的直觉完全不正确。 The actual package customQ and the test client QWithExceptions is buried more deeply in a source tree like so:实际的 package customQ和测试客户端QWithExceptions更深地隐藏在源代码树中,如下所示:

~/learningJava/beginnersGuide/chapter09/tryThis09_01/+
                                                     |
                                                     -QWithExceptions.java
                                                     |
                                                     /customQ/+
                                                              |
                                                              -circularQ.java
                                                              -staticQ.java
                                                              -emptyQException.java
                                                              -fullQException.java

The linter in my development environment suggested that I change the package declaration in customQ/*.java from package customQ;我的开发环境中的 linter 建议我从 package customQ 更改customQ/*.java中的package customQ; to package beginnersGuide.chapter09.tryThis09_01.customQ;package beginnersGuide.chapter09.tryThis09_01.customQ; . . After doing so, I then changed the import statement in the test client to reflect the new package name and moved QWithExceptions.java to ~/learningJava/ and executed $javac QWithExceptions.java , which resulted in a flurry of compilation errors. After doing so, I then changed the import statement in the test client to reflect the new package name and moved QWithExceptions.java to ~/learningJava/ and executed $javac QWithExceptions.java , which resulted in a flurry of compilation errors.

I've also tried $javac -cp.:$CLASSPATH QWithExcpetions.java and $javac -d. -cp.:$CLASSPATH QWithExcpetions.java我也试过$javac -cp.:$CLASSPATH QWithExcpetions.java$javac -d. -cp.:$CLASSPATH QWithExcpetions.java $javac -d. -cp.:$CLASSPATH QWithExcpetions.java , as well as moving QWithExceptions.java further up and further down the source tree one directory at a time, all with various errors. $javac -d. -cp.:$CLASSPATH QWithExcpetions.java ,以及将QWithExceptions.java进一步向上和向下移动源树一次一个目录,所有这些都有各种错误。

At the moment, I'm grappling with a bad class file error.目前,我正在努力解决一个bad class file错误。 While I understand that the compiler wants me to move the offending file somewhere else in the file tree (or perhaps modify the package declarations or import statements) I'm not sure how to do this without invoking a package does not exist error.虽然我知道编译器希望我将有问题的文件移动到文件树中的其他位置(或者可能修改 package 声明或导入语句),但我不确定如何在不调用package does not exist错误的情况下执行此操作。

I'm surely missing some essential rule of package writing that isn't (to me) immediately obvious from the trivial implementations found on the broader web or in my Java texts.我肯定错过了 package 写作的一些基本规则,这在更广泛的 web 或我的 ZD52387880E1EA22817A72D375921 文本中发现的琐碎实现中并不明显(对我来说)。 If you know what it is, please share your wisdom.如果你知道它是什么,请分享你的智慧。

I cannot reproduce your issue because you forgot to show the error messages and file content.我无法重现您的问题,因为您忘记显示错误消息和文件内容。 But I can show you how it works:但我可以告诉你它是如何工作的:

My folder structure:我的文件夹结构:

stefan@stefanpc:~/learningjava$ tree
.
├── beginnersGuide
│   └── chapter09
│       └── tryThis09_01
│           └── customQ
│               └── Test.java
└── Main.java

File Test.java:文件测试.java:

package beginnersGuide.chapter09.tryThis09_01.customQ;

public class Test
{
   public int i;
}

File Main.java:文件 Main.java:

import beginnersGuide.chapter09.tryThis09_01.customQ.Test;

public class Main {
    
    Test test;

    public static void main(String[] args){
        System.out.println("Hello");
    }
}

Compile:编译:

stefan@stefanpc:~/learningjava$ javac Main.java 
stefan@stefanpc:~/learningjava$ 

Classes and the related filenames should start with an uppercase character.类和相关文件名应以大写字符开头。 But I assume that this is not your problem cause.但我认为这不是你的问题原因。

Recommendations of Oracle: https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html Recommendations of Oracle: https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html

Java requires a package declaration align with the folder structures. Java 要求 package 声明与文件夹结构对齐。 For example if you have a package abc' declared at the top of a source file, you need to put that path in an a/b/c folder structure.例如,如果您在源文件顶部声明了package abc' ,则需要将该路径放在a/b/c文件夹结构中。 Note that the folder structure is relative to the source root during compilation.请注意,文件夹结构在编译期间是相对于源根目录的。 So if you have your code in a 'src' folder, and try to compile from the context the parent of that 'src', you will get errors because the folder structure looks like src/a/b/c to the compiler.因此,如果您将代码放在“src”文件夹中,并尝试从上下文中编译该“src”的父级,则会出现错误,因为文件夹结构在编译器中看起来像src/a/b/c Likewise, if your compilation context is set to folder 'a', you would get errors because now the folder looks like b/c to the compiler.同样,如果您的编译上下文设置为文件夹“a”,则会出现错误,因为现在该文件夹在编译器中看起来像b/c

This is a common issue for people new to Java.这是 Java 新手的常见问题。 It's meant to make things easy to find but can be an annoyance.它的目的是让事情变得容易找到,但可能会让人烦恼。

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

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