简体   繁体   中英

How to compile java sources?

如何编译所有Java源代码,包括文件夹和子文件夹内部?

The javac command allows you to specify the files to be compiled by listing their pathnames on the command line, or by giving a file (command line syntax "@file") that contains a list of source filenames. In either case, the way you generate the list of filenames will be OS specific. For example, on Linux you would typically use shell globbing or the find utility to build the list; eg

javac <options> */*.java

or

javac <options> `find . -name \*.java`

or

find . -name \*.java > list
javac <options> @list

or something similar.

However, if you have a number of files to compile, you would be better off in the longer term using a Java build tool such as Ant or Maven . In the Ant case, you specify the files to be compiled (etc) as a FileSet using patterns (aka an antpaths) to match the files. In the Maven case, the build tool typically figures out the Java source filenames are for itself, based on your project's directory structure.

Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler. Java byte-code is a platform independent version of machine code; the target machine is the Java VM rather than the underlying architecture. To compile a Java source code file Foo.java, you would do the following:

% javac -g Foo.java

The -g command line option is optional, but I recommend using it as it makes debugging easier.

But why do not use an IDE to handle all this. Eg eclipse or netbeans. There you can manage your source code and build it.

If you use the Maven build tool for Java, it has the ability to automatically compile all the Java sources in a folder and its subfolders; otherwise, you pretty much have to invoke javac with the path to each source file. For an example Maven-based Java project, see the Java Project Template . If you download the template at the link, you can simply use make or mvn package to compile all the java sources into an executable jar file. Note that this requires you to install the Apache Maven2 build system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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