My previous question asked how to compile files with the command JAVAC
. I still don't know how to set the output files of the compiled source files.
-
1`javac` compiles your `.java` files into `.class` files, keeping whatever is before the dot; so there really isn't any setting of the output file involved. Unless you're trying to ask how to create a `jar`? – fetherolfjd Apr 17 '16 at 21:36
-
2Reading the documentation is generally helpful: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html – Mark B Apr 17 '16 at 21:37
-
"This question already has answers here: [How to compile a java project with a terminal/cmd](https://stackoverflow.com/questions/36678116/how-to-compile-a-java-project-with-a-terminal-cmd)". Which answers are those? The only answers are below. – Daniel Le Nov 14 '21 at 07:55
2 Answers
The output of javac
is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)
If you use the -d
command line option, javac
will also use the package of the class to generate a directory hierarchy, with the -d
option specifying the root. Otherwise, each class file will be output in the same directory as the source file from which it was compiled.

- 1,421,763
- 867
- 9,128
- 9,194
-
can you like.. give me a example? of setting the output path of SOURCE to COMPILED SOURCE? [Image of the directories](http://i77.photobucket.com/albums/j53/Hazhir2003/Capture_zpsq9oxb5sx.png) – zin Apr 17 '16 at 21:44
-
1@Hydrox: You don't set the output path of a specific directory. You just compile with a particular directory as the root output directory using the `-d` command line option. You could just compile with `javac -d COMPILED/SOURCE SOURCE/zoomEngine/Main.java` if you're already in the Z1 directory, for example. (It seems odd to have a directory called SOURCE under COMPILED, mind you.) – Jon Skeet Apr 17 '16 at 21:46
From man javac
on my system:
-d directory
Sets the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d /home/myclasses and the class is called com.mypackage.MyClass, then the class file is called /home/myclasses/com/mypackage/MyClass.class. If -d is not specified, javac puts the class file in the same directory as the source file. Note: The directory specified by -d is not automatically added to your user class path.
Hope that helps.

- 33,938
- 5
- 80
- 91