Reputation: 19
I have two classes in the same package(users).
the first class:
package users;
public class Account{
public String username;
public String password;
}
the second class:
package users;
public class employee{
public void login() {
Account a = new Account();
a.username = "MHM";
a.password = "15234785";
System.out.println("Username: " + a.username);
System.out.println("Password: " + a.password);
}
}
In the main page when i called the method(login):
it says to me: java.lang.ClassNotFoundException: Account
package users;
public class Main{
public static void main(String argv[]) {
employee e = new employee();
e.login();
}
}
what is the fix for this error?
note: these classes were written in the default package then i create package users then move them into the new package.... before moving them in the new package they were working good but after moving the problem is occurs.
Upvotes: 0
Views: 1161
Reputation: 4207
As per see this,
First of all keep in mind that, In one/single package maximum 1-class should be public, more than 1-class as a public modifier not possible.
As i seen you code, it seems that you forget Data-type,
make it correct like,
package users;
class Account{
public String username;
public String password;
}
class employee{
public void login() {
Account a = new Account();
a.username = "MHM";
a.password = "15234785";
System.out.println("Username: " + a.username);
System.out.println("Password: " + a.password);
}
}
public class MyMainClass{
public static void main(String [] args){
// block of code...
}
}
If you want more classes a public modifier then do it by make individual package for all public class.
EDITED
package users;
class Account{
public String username;
public String password;
}
class employee{
public void login() {
Account a = new Account();
a.username = "MHM";
a.password = "15234785";
System.out.println("Username: " + a.username);
System.out.println("Password: " + a.password);
}
}
public class MyMain {
public static void main(String[] args) {
employee e = new employee();
e.login();
}
}
/*
Output :
---------------
Username: MHM
Password: 15234785
*/
Upvotes: 0
Reputation: 2287
java.lang.ClassNotFoundException: Account
Your Account
class is not compilable.Change your Account class like this
public class Account{
public String username;
public String password;
}
UPDATE
Tested your code.There is no other issue except this.Its working fine.
Upvotes: 3
Reputation: 3795
Its throwing ClassNotFoundException
because your Account
class has not been compiled . Hence .class
file has not been generated.
To make the compilation successful Account
class as below :
public class Account{
public String username;
public String password;
}
Upvotes: 0
Reputation: 8387
Just do this all it's ok:
public class Account{
public String username;
public String password;
}
The variables should be have a type
Upvotes: 0
Reputation: 5289
Account
class will not compile because username
and password
need the object type. i.e, String
.
public class Account{
public String username;
public String password;
}
Upvotes: 0
Reputation: 2147
Your Account class is not defined properly. You forgot to add the types of the variables "username" and "password". It should probably be:
public class Account{
public String username;
public String password;
}
(Not to mention that public global variables are never a good thing to choose (instead use getters and setters) - but that's a different topic here :) )
Upvotes: 5