Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile source file to a different directory?

Tags:

Is there a way to compile a Java source file (*.java) to a different directory?

If my package file structure is like so:

Mathematics ->   Formulas ->     src ->       // source files containing mathematical formulas...     bin ->       // class files containing mathematical formulas...   Problems ->     src ->       // source files containing mathematical problems...     bin ->       // class files containing mathematical problems... 

I want to separate source and class files to keep the folders organized, but I currently have to copy all the class files from the src folders to the bin folders after every compile.

Is there a way to simplify this process by compiling the class files to another folder in the javac command?

like image 482
Jonathan Lam Avatar asked Aug 25 '13 17:08

Jonathan Lam


People also ask

How to include file in other folder c++?

You can find this option under Project Properties->Configuration Properties->C/C++->General->Additional Include Directories. and having it find it even in lib\headers. You can give the absolute or relative path to the header file in the #include statement.

How compiler locates a file in java?

By default, execution of the "javac" compiler produces . class files in the same directory where the source code files are located. This location for the generated . class files can be changed using the "-d" switch.


1 Answers

Yup, absolutely - use the -d option to specify the output directory:

javac -d bin src/foo/bar/*.java 

Note that the directory you specify is the root of the output structure; the relevant subdirectories will be created automatically to correspond to the package structure of your code.

See the javac documentation for more details.

In this case you'd need to issue one javac command to compile the formulae, and another to compile the problems though - potentially using the formulae bin directory as part of the classpath when compiling the problems.

(You might want to consider using a single source structure but different packages, mind you. You should also consider using an IDE to hide some of this complexity from you - it ends up getting tiresome doing all of this by hand, even though it's not actually hard.)

like image 199
Jon Skeet Avatar answered Dec 25 '22 14:12

Jon Skeet