The default directory for packages

I am new to java. I want to know what is the default directory for packages in java? I mean, if I compile a java file containing the package instruction and I compile it without using the -d option in javac , then where will the package be created? eg.

 package test; class Test{} 

and compile it with javac Test.java
then where will the package be created?

thanks

+4
source share
3 answers

-d in the javac command is used to indicate where to generate the class file, if you do not specify it, then the .class file will be created in the same directory where your current .java file is located.

+3
source

If you do not specify -d , the class file will be created in the same directory as the source file.

This is great if you already save your source in the directory structure corresponding to the structure of your package (and if you are happy that your source and class files live in the same place), but if your source structure does not match your package structure, basically you will get class files in places where they cannot be reasonably used.

Personally for anything other than a quick reset (usually Stack Overflow :) code I would make the following suggestions:

  • Avoid using the default package
  • Store the source code in a directory structure (e.g. with the root src ) corresponding to the package structure
  • Generate class files in a separate directory structure (for example, with the root of bin or out or classes )

(Sorry, I read the question incorrectly to start with.)

+4
source

There is no directory created unless you specify a package! all .class files will be created directly in the output folder

 public class A{} 

if you compile this to display the folder,

output/a.class created

0
source

All Articles