简体   繁体   English

Java:从命令行编译并运行多个软件包

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

I created multiple packages and want to compile and run them. 我创建了多个软件包,并希望对其进行编译和运行。 I fiddled around with javac and java and learned about how packages should be named and how a project should be structured. 我摆弄了javacjava ,了解了如何命名软件包以及如何构建项目。 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. 我知道我可以为此使用IDE,但是出于好奇,我想使用命令行工具进行尝试。 Here is how my project is organized: 这是我的项目的组织方式:

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". 问题是当我尝试编译“ Program.java”(即我从命令行运行的测试类)时,编译器将引发错误,因为它找不到包“ net.chris.dojo.datastructures”和“ net” .chris.dojo.sorting”。 Here is the compilation command: 这是编译命令:

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 LinkedList.java

package net.chris.dojo.datastructures;

public class LinkedList {
    ...
}

MergeSort.java MergeSort.java

package net.chris.dojo.sorting;

public class MergeSort {
    ...
}

Program.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. 谢谢你的帮助。

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提供解决方案。

Try change this in your Program class 尝试在您的程序类中更改此设置

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

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

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