Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

compilation - How to compile multiple Java files when there are Java files in other packages?

I am compiling multiple files in a directory (javac *.java) but I have a problem when I try to do this.

I get compile errors saying that javac cannot find a symbol of an object.

I have multiple packages that contain Java files that are needed to run the main program. But it seems that trying to compile these one by one won't work. It runs fine in my IDE but I'm interested in learning how it's done via command prompt.

The main program is in the drivers folder. I have tried compiling the files in order of dependency but that didn't work out.

this is a screenshot of the folders

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Javac documentation provides all the necessary information. However, it might be useful to use Ant or Maven for command line builds.

This page provides a good example of using first javac and then Ant for building a simple project.


Here is an example project and how can it be compiled with javac.

The tree structure of the project is this:

   .
    ├── build
    └── src
        ├── attacks
        ├── drivers
        │?? └── Driver.java
        └── exceptions
            └── MyException.java

There are two special directories -- build for containing compiled classes and src to contain source files (could be in different subdirectories -- packages).

The following command compiles the whole project and puts the result into the build directory.

javac -sourcepath src -d build src/**/*.java

The -sourcepath src specifies directory src as the place where all the source can be found by the compiler. The -d build options tells the compiler where to place the compiled files.

Option src/**/*.java tells the compiler what files to actually compile. In this specific case it tells javac to look two levels down and pick all *.java at that level.

If there are *.java files at different levels than a list of files needs to be specified. For this, one could create such listing as an external file and pass this files as in input option for javac.

Here is how this could be done under Linux/Unix:

find -name "*.java" > source.txt

The above command creates file source.txt that contains full paths for the found *.java files. For this example it contains:

./src/drivers/Driver.java
./src/exceptions/MyException.java

In order to compile the project with the list of source files flushed into source.txt, the following command can be used:

javac -d build @source.txt

Please note that @source.txt specified at the end that tells the compiler where to look for a list of source files. Please also note that the -sourcepath option can be omitted.

Here is how the directory structure changed after running the above command.

.
├── build
│?? ├── drivers
│?? │?? └── Driver.class
│?? └── exceptions
│??     └── MyException.class
└── src
    ├── attacks
    ├── drivers
    │?? └── Driver.java
    └── exceptions
        └── MyException.java

As can be observed the build directory now contains the compiled class files in respective packages.

And, if you'd like to run it, assuming, for example, that Driver has method main, the following command executes the program.

java -cp .:build:**/*.class drivers.Driver

Please note that file separator : (colon) is used under Unix, for Windows change it to ; (semicolon).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...