user197122
user197122

Reputation: 87

Default Directory for packages

I am a newbie in java I Want to know that what is the default directory for packages in java ? I Mean if i compile a java file which contains a package statement,and i compile it without using -d option in javac command,then where will be the package created ? eg.

package test;
class Test{}

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

Thanks

Upvotes: 4

Views: 138

Answers (3)

Shivam Dhabhai
Shivam Dhabhai

Reputation: 331

-d option in javac command is use to specify where to generate the class file,if you don't specify it,then the .class file will be created in the same directory where your .java file is present.

Upvotes: 3

user2269177
user2269177

Reputation: 13

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

public class A{}

if you compile this to output folder ,

output/a.class is created

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500375

If you don't specify -d, the class file will be created in the same directory as the source file.

That's fine if you're already storing your source in a directory structure matching your package structure (and if you're happy for your source and class files to live in the same place) but if your source structure doesn't match your package structure, you'll basically end up with class files in locations where they can't sensibly be used.

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

  • Avoid using the default package
  • Keep your source code in a directory structure (e.g. with a root of src) matching package structure
  • Generate class files into a separate directory structure (e.g. with a root of bin or out or classes)

(Sorry, misread the question to start with.)

Upvotes: 4

Related Questions