Java:从命令行编译和运行多个包 [英] Java: Compiling and running multiple packages from the command-line

查看:653
本文介绍了Java:从命令行编译和运行多个包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了多个包,并希望编译和运行它们。我用 javac java 拼凑,并了解如何命名包以及如何构建项目。我希望我没事。但我无法编译和运行的东西。我知道我可以使用IDE为此,但我想尝试它的命令行工具只是为了好奇。
这是我的项目组织方式:

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. 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:

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

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

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.

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

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

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:

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

这是我得到的输出:

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


b $ b

这是我的类文件的代码:

Thats the code of my class files:

Queue.java

Queue.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

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();
    }

}

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

感谢@BigMike的解决方案。

Thanks @BigMike for the solution.

这篇关于Java:从命令行编译和运行多个包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆