Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NoSuchMethodError: main - What am I doing wrong?

 
Bob Mann
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created the following class but I keep getting the following error message:
========================================================
Exception in thread "main" java.lang.NoSuchMethodError: main
========================================================
class myClass
{
public void print()
{
System.out.println("M.A");
}
}

public class xxx
{
public static void main(String arg[])
{
myClass test = new myClass();
test.print();
}

}

=======================
Many thanks in advanced
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably trying to execute myClass:

Since the main method is in xxx, you must execute xxx:
 
Alex Collins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you have not quite grasped the idea of method's and classes in java.
You do not need to classes here.
This will do the trick:
public class myClass{
public void printMyText(){
System.out.println("M.A");
}
public static void main(String[] args){
printMyText();
}
}
save,exit and then compile this program using:
>javac myClass.java
then run it using java:
java myClass
That will work.
Hope you understood
P.S:You really do not need to put the System.out.println in its separate method as there is very little code. However it is a good example of using methods...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic