Java:从命令行编译和运行多个包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18913119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:16:52  来源:igfitidea点击:

Java: Compiling and running multiple packages from the command-line

javacommand-linecompilationjavac

提问by urbaindepuce

I created multiple packages and want to compile and run them. I fiddled around with javacand javaand learned about how packages should be named and how a project should be structured. I hope I got all right. But I fail at compilation and running the stuff. I know I could use an IDE for this, but I want to try it with the command-line tools just for curiousity. Here is how my project is organized:

我创建了多个包并希望编译和运行它们。我摆弄周围javacjava并了解项目如何包应该被命名为和应该如何构建。我希望我一切都好。但是我在编译和运行这些东西时失败了。我知道我可以为此使用 IDE,但出于好奇,我想使用命令行工具进行尝试。这是我的项目的组织方式:

Project
  + src
    + net
      + chris
        + dojo
            - Program.java
          + datastructures
            - Queue.java
            - LinkedList.java
          + sorting
            - MergeSort.java
  + bin
    + net
      + chris
        + dojo
            - Program.class (should be here but missing because compilation fails)
          + datastructures
            - Queue.class
            - LinkedList.class
          + sorting
            - MergeSort.class

Compilation for the classes in the "datastructures" and "sorting" packages is working fine. Here are the commands I used. The folder structure in the "bin" folder is automatically created by the compiler.

“数据结构”和“排序”包中的类的编译工作正常。这是我使用的命令。“bin”文件夹中的文件夹结构是由编译器自动创建的。

javac -d bin src\net\chris\dojo\datastructures\*.java
javac -d bin src\net\chris\dojo\sorting\*.java

The problem is when I try to compile "Program.java" (thats the test class I run from the command-line) the compiler is throwing errors, because it cannot find the packages "net.chris.dojo.datastructures" and "net.chris.dojo.sorting". Here is the compilation command:

问题是当我尝试编译“Program.java”(这是我从命令行运行的测试类)时,编译器抛出错误,因为它找不到包“net.chris.dojo.datastructures”和“net .chris.dojo.sorting”。下面是编译命令:

javac -d bin src\net\chris\dojo\Program.java

This is the output I get:

这是我得到的输出:

src\net\chris\dojo\Program.java:3: error: cannot find symbol
import net.chris.dojo.datastructures;
                     ^
symbol:   class datastructures
location: package net.chris.dojo
src\net\chris\dojo\Program.java:4: error: cannot find symbol
import net.chris.dojo.sorting;
                     ^
symbol:   class sorting
location: package net.chris.dojo
src\net\chris\dojo\Program.java:11: error: cannot find symbol
            MergeSort.sort(values);
            ^
symbol:   variable MergeSort
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
            Queue queue = new Queue();
            ^
symbol:   class Queue
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
            Queue queue = new Queue();
                              ^
symbol:   class Queue
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
            LinkedList list = new LinkedList();
            ^
symbol:   class LinkedList
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
            LinkedList list = new LinkedList();
                                  ^
symbol:   class LinkedList
location: class Program
7 errors

Thats the code of my class files:

这就是我的类文件的代码:

Queue.java

队列.java

package net.chris.dojo.datastructures;

public class Queue {
    ...
}

LinkedList.java

链表

package net.chris.dojo.datastructures;

public class LinkedList {
    ...
}

MergeSort.java

合并排序程序

package net.chris.dojo.sorting;

public class MergeSort {
    ...
}

Program.java

程序.java

package net.chris.dojo;

import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;

public class Program {

    public static void main(String[] args) {
        int[] values = { 9, 4, 6, 2, 0, 3, 8, 1, 7, 5 };
        MergeSort.sort(values);
        Queue queue = new Queue();
        LinkedList list = new LinkedList();
    }

}

I would run it with this command:

我会用这个命令运行它:

java -cp bin net.chris.dojo.Program

I execute all commands in the root folder of the project. Thanks for your help.

我在项目的根文件夹中执行所有命令。谢谢你的帮助。

采纳答案by urbaindepuce

The solution was to include the classpath when compiling. That way it can find the packages it depends on.

解决方案是在编译时包含类路径。这样它就可以找到它所依赖的包。

javac -d bin -cp bin src\net\chris\dojo\Program.java

Thanks @BigMike for the solution.

感谢@BigMike 的解决方案。

回答by Alexey Odintsov

Try change this in your Program class

尝试在您的 Program 课程中更改此设置

import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;

to

import net.chris.dojo.datastructures.*;
import net.chris.dojo.sorting.*;

And when you compile your Program.java use following command

当您编译 Program.java 时,请使用以下命令

javac -d bin src\net\chris\dojo\Program.java -classpath bin