9 Replies - 8079 Views - Last Post: 05 April 2012 - 02:51 PM Rate Topic: -----

#1 samerina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 17-February 12

Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 01:30 PM

Slightly baffled.


 class Program8
 {
 	public static void main(String[] args)
 	{
	NewPerson suzi = new NewPerson("Suzi", "123 Test St", "123-234-3456", "suzi@suzikins.org");
	suzi.displayPerson();
 	}

 }

class NewPerson
{
	private String name = "name";
	private String address = "address";
	private String phone = "phone";
	private String email = "email";

	NewPerson(String name, String address, String phone, String email)
	{
		this.name = name;
		this.address = address;
		this.phone = phone;
		this.email = email;
	}

	String name(String aName)
	{
		name = aName;
		return name;
	}

	String address(String anAddress)
	{
		address = anAddress;
		return address;
	}

	String phone(String aPhone)
	{
		phone = aPhone;
		return phone;
	}

	String email(String anEmail)
	{
		email = anEmail;
		return email;
	}
	public void displayPerson()
	{
		System.out.println("Name: " + name);
		System.out.println("Address: " + address);
		System.out.println("Phone: " + phone);
		System.out.println("Email: " + email);
	}
}



Whenever I compile I get that runtime error, but I clearly have a main class!

Is This A Good Question/Topic? 0
  • +

Replies To: Runtime Error: Could not find or load main class Main

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 01:34 PM

What's the name of your .java file that contains the code you've posted?
Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 01:49 PM

Do you also have each class in their own file -

'Program8.java' and
'NewPerson.java'

?
Was This Post Helpful? 0
  • +
  • -

#4 samerina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 17-February 12

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 01:58 PM

Yeah I do. I just posted it together here so you could see everything I have.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 02:06 PM

The JRE is looking for a class called Main, not a method, and it is expecting to find it because it has been told to look for it, probably because it received a command like:

java Main

How are you compiling and running? What commands did you use, etc. We need more info.
Was This Post Helpful? 0
  • +
  • -

#6 samerina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 17-February 12

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 02:29 PM

Well, I tried compiling and running it in the JCreator tool and got the same error. I tried on ideone.com as well. So I tried it in the command prompt with "java -cp . Program8", "java -classpath . program8", "java program8.java", and several other variations on that. It creates a .class file for me and no errors when compiling. I just get the error when attempting to run it. They're all in the same folder, and I changed my directory to get there. I'm pretty new to coding, so I'm not sure where I've gone wrong.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 02:40 PM

Well, we can't look over your shoulder to see how you're doing everything, so here's another approach: giving you code that works.

I took your code, left it in one file called Program8.java, because that was easiest, and ran it to verify that it compiles and runs as it should. I've posted the code below. You should be able to copy it to a new or over your existing Program8 class in a file called Program8.java, compile, and run it. If you can't, please post your errors and let us know. If you can run it, the experience will hopefully explain what's going wrong with your current code.
class Program8
{
    public static void main(String[] args)
    {
        NewPerson suzi = new NewPerson("Suzi", "123 Test St", "123-234-3456", "suzi@suzikins.org");
        suzi.displayPerson();
    }

}

class NewPerson
{
    private String name = "name";
    private String address = "address";
    private String phone = "phone";
    private String email = "email";

    NewPerson(String name, String address, String phone, String email)
    {
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.email = email;
    }

    String name(String aName)
    {
        name = aName;
        return name;
    }

    String address(String anAddress)
    {
        address = anAddress;
        return address;
    }

    String phone(String aPhone)
    {
        phone = aPhone;
        return phone;
    }

    String email(String anEmail)
    {
        email = anEmail;
        return email;
    }
    public void displayPerson()
    {
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Phone: " + phone);
        System.out.println("Email: " + email);
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 02:44 PM

Your error message is quite clear

You are trying to run a java class Main that does not have a main() method

If you have a Main class it should be in a file named Main.java

You should post your error stack
it will shows which .class the JRE tries to access
Was This Post Helpful? 0
  • +
  • -

#9 samerina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 17-February 12

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 02:49 PM

I got the exact same error when I ran it and it looks the exact same as my code, so I am guessing I am doing something wrong when attempting to run it. Is it ok for you to tell me how you ran it? I compiled it as all one file to see if that would help.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Runtime Error: Could not find or load main class Main

Posted 05 April 2012 - 02:51 PM

Post your stack trace
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1