0

I am working on an online Java Course. We are presently working on building the following instantiation code:

public class NameDriver
{
   public static void main (String[] args)
   {
      //Instantiation
      Name myName = new Name("Scott", "Robert", "Mitchell");
      Name myName1 = new Name("Scott", "Mitchell");
      Name myName2 = new Name("Mitchell");
      Name noName;
      System.out.println("myName: " + myName.toString());
   }
}

For the following the following:

public class Name
{

   private String first;
   private String middle;
   private String last;

   //Constructor Methods
   public Name(String f, String m, String l)
   {
      first = f;
      middle = m;
      last = l;
   }

   public Name(String f, String l)
   {
      first = f;
      middle = "";
      last = l;
   }

   public Name(String l)
   {
      first = "";
      middle = "";
      last = l;
   }

   public Name()
   {
      first = "";
      middle = "";
      last = "";
   }

   public String toString()
   {
      return first + " " + middle + " " + last;
   }
 }

The result when I execute is the error message "Error: Could not find or load main class".

The names of the Java files duplicate the name of the Main Class, so that doesn't seem to be the problem.

I have done a fair bit of research and the recurring theme appears to be that I need to specify a class path using the -cp option. I have attempted this using the complete path name as well as the '.' from the directory in which the codes are located, but to no avail. It is also worth mentioning that the code appears to compile successfully and that the error occurs on execution.

There is a good possibility that I have messed up the code - as I have only just started using Java, I just can't see it, so another set of eyes would be great.

6
  • did you compile the files to create the .class files using javac Commented May 3, 2013 at 3:38
  • do you have a package definition in the page Commented May 3, 2013 at 3:52
  • you need to use java NameDriver command to run the file and it has to be from the folder in which you have kept the .class files. ie in cmd you need to navigate to that folder and then run it. I just tried it and it is working fine Commented May 3, 2013 at 3:54
  • I am doing just that Arun. What is a package definition? I have included all code used by the 2 .Java files. Commented May 3, 2013 at 4:53
  • what are the name of the files Commented May 3, 2013 at 4:57

1 Answer 1

1

1) Its Java not Javascript

2) You are not compiling and executing it the right way.

ie:

Open command window. Navigate to the folder where your java source files are. And then Run Javac command to compile them.

For ex: Javac *.java

3) Next, to run,

Navigate to the root class folder(where class files are generated). If you are using source dir as root folder for compiled classes then its alright.

Run Java NameDriver to run your program.

Answer Updated:

1) I think you are only compiling main class NameDriver. You need to compile all classes.

For ex: javac *.java. This will compile all classes.

2) You are running it wrong.

You can't mention .java when running a class file. This is wrong >> java -cp . NameDriver.java

This is right >> java NameDriver

Sign up to request clarification or add additional context in comments.

4 Comments

1) Sorry slip of the finger. 2 & 3) I have been compiling and executing the code exactly as you have stated.
@user2337871, post your complete directory/file structure. Tell me how you are running your program.
The directory I am in is F:\Java, which is where all my .java files are located and I am using the following to compile javac -cp . NameDriver.java and java -cp . NameDriver.java to execute.
@user2337871, you are running it wrong. check updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.