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.
javacjava NameDrivercommand 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