0
package emailapp;
import java.util.Scanner;

public class Email {

    private String firstName;
    private String lastName;
    private String department;
    private String password;
    private String alternativeEmail;
    private int mailboxCapacity = 200;
    private String email;
    private String companyName = "HomeCo.com";

    public Email(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
        //System.out.println("Email Created!:" + this.firstName + " " + this.lastName + " ");

    this.department = setDepartment();
    //System.out.println("Your Email Department: "+ this.department);
    this.password = setPassword(8);
    //System.out.println("Your Password Is: "+ this.password);
    email = firstName.toLowerCase() + "." +  lastName.toLowerCase() + "@" + department + "." + companyName;
    //System.out.println("Your Email Address Is: "+ email);
    }



    private String setDepartment() {

        System.out.print("CHOICES:\n1.Sales\n2.Development\n3.Accounting\n4.None Of The Above\n");
        Scanner Input = new Scanner(System.in);
        int choice = Input.nextInt();

        if(choice == 1) {
            return "sales";
        }
        else if(choice == 2) {
            return "dev";
        }
        else if(choice == 3){
            return "acct";
        }
        else {
            return " ";
        }
    }

package emailapp;
public class EmailApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Email em1 = new Email("Rojin","Ebrahimi");
        System.out.println(em1.showInfo());
    }

}

I have written a mini email-generating app which uses 2 classes: a class called "Email" and another class called "EmailApp" (which includes the main one). It has been written in Eclipse and I'm trying to run my codes in cmd but I'm confused how to first compile it.

When I type:

javac EmailApp.java,

it keeps telling me this:

class Email is public, should be declared in a file named Email.java

These classes are inside a package called "emailapp.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Rojin
  • 1,045
  • 9
  • 15
  • Beyond the fact that a public class X needs to live in X.java ... your source code tree structure has to reflect your package structure, too. In your case, that would mean that you have **two** java files sitting in a directory called emailapp. And that you then do `javac emailapp/*.java` for example . – GhostCat Mar 17 '19 at 19:27
  • And for the record. please make sure that **all** your source code is correctly formatted and indented. – GhostCat Mar 17 '19 at 19:28
  • It all works in Eclipse! I just don't know how to run it from cmd and I get errors. I tried the solution you suggested, but it still doesn't work. – Rojin Mar 17 '19 at 19:42
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. In other words: reduce your input to two (maybe almost empty classes, just enough so it should compile fine). Then describe your project setup (source code locations, directory names) and the exact commands you are using. And as I just told you: `javac X.java` is **wrong**. – GhostCat Mar 17 '19 at 19:46

2 Answers2

0

The error means your file name and class name have different names. You should keep a separate file for each class and make sure each file has the same name of the class it contains.

for example: File: EmailApp.java contains: public class EmailApp { ... }

File: Email.java contains: public class Email { ... }

Now, when you compile the class with the main method, it will also compile the other class - assuming EmailApp calls Email. The main method should look like this:

public static void main(String... args) {
 // 
}

So, you compile only the EmailApp.java as you did.

In any case, please provide the files you work on..

Pam Stums
  • 176
  • 8
  • Thank you for answering but I still got an error. Actually, the class of Email is built by compiling it but it tells me that : could not find or load main class EmailApp. Is there any other way to solve this problem? – Rojin Mar 17 '19 at 19:01
  • *Now, the class you compile, must have the main method. e.g.* Wrong. The only reason you **need** a public static void main is: when you want to **run** that class. To compile a class, it doesnt need a main method. – GhostCat Mar 17 '19 at 19:29
  • @GhostCat, correct! I've modified and explained myself. I meant, compiling the class with the main is enough and no need to compile separately the other class(es)... then it should run ok, assuming he wrote the calls correctly (i.e. EmailApp with main which calls Email...) – Pam Stums Mar 17 '19 at 20:34
  • It works in Eclipse and I have tried different ways but were no use. – Rojin Mar 17 '19 at 20:41
  • Thank You All It Finally Worked!! – Rojin Mar 17 '19 at 21:13
0

Well, you need to do exactly what this error tells you to. In Java, every public class (except inner classes) needs to have it's own file.

You have probably put the code for both classes in one file named EmailApp.java. But you need to put all the code for the Email class in a separate file named Email.java.

After doing this can you compile both files in the command line using following commands:

javac EmailApp.java
javac Email.java

And then you run the class in which you've put your main-function (which probably will be the EmailApp.java file:

java EmailApp

Update after comment:
Are you sure that cmd is in the right directory? In your File Explorer, search for the directory where the source files are. You should be able to copy the path from the address bar in the File Explorer. In cmd you then should perform a change of directory with the cd command:

cd "put the path to the right directory in here"

Note: if there are white spaces in the path, make sure to put your path within quotation marks.

bramvdwalle
  • 79
  • 1
  • 10