从多个目录编译java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24847436/
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-14 15:11:23  来源:igfitidea点击:

Compile java from multiple directories

javajavac

提问by dyl

I'm trying to compile multiple .java programs from different directories either on Windows, Mac, or Linux... e.g. cmd or terminal. It doesn't matter.

我正在尝试从 Windows、Mac 或 Linux 上的不同目录编译多个 .java 程序……例如 cmd 或终端。没关系。

However, I'm sure that many of you are familiar with how Netbeans stores files in different folders. I have been putting different concepts into different folders, and now I want to run all of them.

但是,我相信你们中的许多人都熟悉 Netbeans 如何将文件存储在不同的文件夹中。我一直将不同的概念放入不同的文件夹中,现在我想运行所有这些。

For example, a chess program that I have looks sort of like this:

例如,我的国际象棋程序看起来像这样:

/Chess
    /build
        /classes
            /chess
                /Chess.class
            /color
                /colorHelper.class
            /game
                /Board.class
                /Game.class
                /GameManager.class
                /Player.class
            ... etc. (the rest of the directories with .class files)
    /build.xml
    /gameLog.txt
    /manifest.mf
    /nbproject
        ... (some xml and .properties files)
    /src
        /chess
            /Chess.java
        /color
            /ColorHelper.java
        /game
            /Board.java
            /Game.java
            /GameManager.java
            /Player.java
        ... etc. (the rest of the directories with .java files)

So, my question is, how can you use javac *.java(in /srcprobably) to compile all of the files (because otherwise I get a cannot find symbolerror. Since I get a file not foundwhen I run javac *.javain src, I am at a loss.

所以,我的问题是,你怎么能使用javac *.java(在/src可能)编译的所有文件(因为否则我得到一个cannot find symbol错误,因为我得到一个file not found当我运行javac *.javasrc,我很茫然。

Thanks in advance,

提前致谢,

Dylan

迪伦

采纳答案by Am_I_Helpful

It won't compile in one go.Actually,your srcdirectory doesn't contain any .java file,SO it won't be done in that way!

它不会一次性编译。实际上,您的src目录不包含任何 .java 文件,因此不会以这种方式完成!

I am afraid that you'll have to do it by changing your path under srcfolder to do the same.

恐怕您必须通过更改src文件夹下的路径来执行相同的操作。

You'll have to perform for each chess,color,game,etc. directories to achieve the same.

你必须为每个执行chesscolorgame,等。目录来实现相同的。

So,change path at each run or go as advised in the comments to achieve compilation of all the java files.

因此,在每次运行时更改路径或按照注释中的建议进行以实现所有 java 文件的编译。

OR

或者

As proposed by David Ehrmann in your comment,you can do it by compiling in one go using javac $(find . -name '*.java').

正如 David Ehrmann 在您的评论中所建议的,您可以使用javac $(find . -name '*.java').

It will compile all .java files under your present directory(src(.)).

它将编译您当前目录下的所有 .java 文件(src(.))。

回答by Inder

javac *.java compiles all the Java files within the current directory. In your case, since you have different hierarchy of folders based on the package, you cannot achieve this in one go. At best you can run the command javac *.java on each folder.

javac *.java 编译当前目录中的所有 Java 文件。在您的情况下,由于您基于包具有不同的文件夹层次结构,因此您无法一次性实现此目的。您最多可以在每个文件夹上运行 javac *.java 命令。

回答by masstroy

You should try using an automated build system like maven or ant. It will allow you to build your project with a single command. It will even allow running unit tests, and packaging your application as a jar (or whatever package you prefer).

您应该尝试使用像 maven 或 ant 这样的自动构建系统。它将允许您使用单个命令构建项目。它甚至允许运行单元测试,并将您的应用程序打包为 jar(或您喜欢的任何包)。

If you really don't want to use one of those you can still do it with a single (albeit lengthy) command. The *.java is just a wildcard in a file path. Its looking for any files in the current directory ending with .java. On linux you can try something like this find -name "*.java" | xargs javac. The first part is a find command that searches recursively for all files ending with .java. It this pipe's that list of files to xargs which adds them all as individual arguments to the java command.

如果您真的不想使用其中之一,您仍然可以使用单个(尽管很长)命令来完成。*.java 只是文件路径中的通配符。它在当前目录中查找以 .java 结尾的任何文件。在 linux 上,您可以尝试这样的操作find -name "*.java" | xargs javac。第一部分是一个 find 命令,它递归地搜索所有以 .java 结尾的文件。这个管道是 xargs 的文件列表,它将它们全部作为单独的参数添加到 java 命令中。

That really isn't the best way. Again, try maven. Its pretty simple to set up for a basic project, your IDE usually has a feature to create a maven project for you as well.

那确实不是最好的办法。再次尝试maven。为基本项目设置非常简单,您的 IDE 通常也具有为您创建 Maven 项目的功能。

回答by Vivek Barai

to compile more than one file having different package name in same folder use

在同一文件夹中编译多个具有不同包名的文件使用

javac -d . *.java