开发者

How to compile java sources?

How to compile all Java source code including insi开发者_Python百科de folders and subfolders?


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; e.g.

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. E.g. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜