0

I have made a console based banking system in my first Java assignment. Everything works well if I use default package but when I add a

   package bank;

at the top of all classes, it can not find classes and there are a lot of errors. How can I solve it?

I searched a lot on Google but didn't understand this thing.

cmd path is ...

C:\Users\Shah\Desktop\Assignment1\src\Bank.java and other classes are also in the same folder.

kindly help me to understand this thing

I tried

     javac *.java

and it compiled

but when I tried to run the byte code by

     java Bank

it gives java.Lang.NoClassDefFound Error

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Charlie
  • 4,827
  • 2
  • 31
  • 55

2 Answers2

1

If you have just added the package line in you classes.java,

package bank;

then you need to change the structure of your applicationm by adding a new folder named bank then you classes.java that you have been add the link package in.

so you cmd path should look like this :

C:\Users\Shah\Desktop\Assignment1\src\bank\Bank.java
Salah
  • 8,567
  • 3
  • 26
  • 43
  • i added the bank folder and added all classes in it but it is still not working – Charlie Feb 19 '14 at 17:51
  • You need also to check the cmd path as i mentioned above. – Salah Feb 19 '14 at 17:55
  • i changed it till bank – Charlie Feb 19 '14 at 17:58
  • From your update, when you tried `javac *.java`, you are telling the compliter to comple any class exsit in any package, but when tried `javac Bank`, this considering the defual package, and i belive if you said `javac bank.Bank` it will work, you have to tell the compiler from which package compile classes. – Salah Feb 19 '14 at 18:12
1

I think you need to create another folder 'bank' corresponding to bank package under src folder. src/bank

place all classes under src/bank which defines package bank;

Also you can compile mutiple files using javac com\pack1\pack2\one\*.java com\pack1\pack2\two\.*java

then try to run.


For example image below shows package pack1 which contains a Test.java class with package definition Image shows compiling different java classes

For more info refer this question

Community
  • 1
  • 1
Nithin CV
  • 1,046
  • 9
  • 23