-1
package com;

class Account {

    public double balance;

    public Account(double initBalance) {
       balance = initBalance;
    }
}

Account.class file is generated but it in same folder. File sub folder named 'com' is not created. Actually I tried to compile directly . Now I can see its totally wrong to compile a java file folder without specifying root directory . ( -d . )

Aditya Yada
  • 168
  • 1
  • 2
  • 9

2 Answers2

1

If you are compiling your code from command line, then you can use the following command to automatically create the folder corresponding to your packages: -

javac -d . Account.java

This will create a folder com in the current folder, where your Account.java is placed, and move the class file automatically to that folder.

Note that, packages are there for your class files. You can place your .java file anywhere. But in addition, your corresponding .class file should be inside a folder corresponding to your package in your .java file.

For e.g, see the below directory structure: -

root --+ Account.java 
       |
       +-- com --Account.class  (com is the package folder)

So, the fully qualified name of your Account class becomes - com.Account.

Then in order for your Account class to be found by other class, set the classpath till the com folder ( Note: - Don't include the com folder in classpath).

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

First of all, package is nothing, it's a folder (non-technically). So be sure, your .java is created inside com folder/package. Also, com is not a part of any folder/package. If you are working on some IDE like Netbeans, then right click on package and New then java class.

Update

If you are not using any IDE, then check your class path. If E:\data\fold\java1mods\Module2\excercise4\BankPrj\ is your class path. Then, create com folder and create your .java file inside same directory i.e. com.

Ravi
  • 30,829
  • 42
  • 119
  • 173