why folder structure is not created even though I had taken package statement?

I was trying a package concept in java.

I created a Test.java file that only contains a single statement:

package pack1;

When I compile the file using the command as:

javac -d . Test.java

it compiles fine but doesn't create the .class file, nor a folder named pack1.

I don't know why. Can anyone please suggest?

If I change the code to:

package pack1;
class Test{}

and compile the program by using the command

javac -d . Test.java

Then it compiles fine and folder structure is also created correctly.

Please suggest me why it happens that way?

Jon Skeet
people
quotationmark

In your original code, you haven't declared any classes - so there wouldn't be any class files. If there are no class files, presumably the compiler sees no need to create the directory structure that would have been necessary for any class files it would have created.

In practice, the only time I've ever seen a Java source file with no classes in is for package information - and that's pretty pointless if there are no classes in the package, too, although it's possible that if you run javadoc it will create an entry for the empty package. As noted in comments, package annotations will end up creating a class file, but they have to be in package-info.java. For example:

// In package-info.java
@Deprecated
package foo;

Compiling that will create package-info.class, creating a directory for it if necessary.

people

See more on this question at Stackoverflow