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

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

问题描述

我创建了多个包并想编译和运行它们.我摆弄了 javacjava 并了解了包应该如何命名以及项目应该如何构建.我希望我一切都好.但是我在编译和运行这些东西时失败了.我知道我可以为此使用 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
etchrisdojodatastructures*.java
javac -d bin src
etchrisdojosorting*.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
etchrisdojoProgram.java

这是我得到的输出:

src
etchrisdojoProgram.java:3: error: cannot find symbol
import net.chris.dojo.datastructures;
                     ^
symbol:   class datastructures
location: package net.chris.dojo
src
etchrisdojoProgram.java:4: error: cannot find symbol
import net.chris.dojo.sorting;
                     ^
symbol:   class sorting
location: package net.chris.dojo
src
etchrisdojoProgram.java:11: error: cannot find symbol
            MergeSort.sort(values);
            ^
symbol:   variable MergeSort
location: class Program
src
etchrisdojoProgram.java:12: error: cannot find symbol
            Queue queue = new Queue();
            ^
symbol:   class Queue
location: class Program
src
etchrisdojoProgram.java:12: error: cannot find symbol
            Queue queue = new Queue();
                              ^
symbol:   class Queue
location: class Program
src
etchrisdojoProgram.java:13: error: cannot find symbol
            LinkedList list = new LinkedList();
            ^
symbol:   class LinkedList
location: class Program
src
etchrisdojoProgram.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:

队列.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 {
    ...
}

程序.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
etchrisdojoProgram.java

感谢@BigMike 的解决方案.

Thanks @BigMike for the solution.

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

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