faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

61 of 111 people (55%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Java: Programming: Stand alone: Error: Exception in thread "main" java.lang.NoSuchMethodError: main

Aug 31st, 2006 07:08
Knud van Eeden, Cock Roach,


----------------------------------------------------------------------
--- Knud van Eeden --- 11 January 2004 - 02:59 am --------------------

Java: Programming: Stand alone: Error: Exception in thread "main" 
java.lang.NoSuchMethodError: main

---

Possible cause: you should name your Java with the same name as the 
class in which you find the main method.

For example:

you should save this Java file as

 CLASSStringConvertInfixToSuffixMain.java

(and thus not as CLASSStringConvertInfixToSuffix.java, that will give 
this error)

--- cut here: begin --------------------------------------------------

 class CLASSStringConvertInfixToSuffix {
 }

 class CLASSStringConvertInfixToSuffixMain {
  public static void main( String args[] ) {
  }
 }

--- cut here: end ----------------------------------------------------

===

Possible cause: you are trying to run an applet class file using 
java.exe

For example:

If you compile this source code using javac.exe to a .class file,
and then run that .class file using java.exe, you will get that error.

--- cut here: begin --------------------------------------------------

 import java.applet.*;
 import java.awt.*;
 import java.awt.event.*;
 //
 public class ComponentCreateButton extends Applet implements 
ActionListener {
  //
  Button button1 = null;
  //
  public void init() {
   button1 = new Button( "Press this button" );

   add( button1 );
   button1.addActionListener( this );
  }
  //
  public void actionPerformed( ActionEvent ev ) {
   setBackground( Color.red );
  }
 }

--- cut here: end ----------------------------------------------------

Solution:

When running an applet, you should call it from an HTML file instead

e.g.

--- cut here: begin --------------------------------------------------

<HTML>
  <APPLET
    CODE="ComponentCreateButton.class"
    WIDTH="300"
    HEIGHT="200"
  >
  </APPLET>
</HTML>

--- cut here: end ----------------------------------------------------

===

Possible cause: you should include the main parameters args.

For example:

class Mytestclassname {
 public static void main() {
 }
}

Consistently gave
this error:

+----------------------------------------------------------------+
|C:\TEMP>c:\j2sdk1.4.1_03\bin\java -classpath c:\temp Myclassname|
|Exception in thread "main" java.lang.NoSuchMethodError: main    |
+----------------------------------------------------------------+

Solution:

Include the 'String args[]' as parameter of 'main()'.

For example:

class Mytestclassname {
 public static void main( String args[] ) {
 }
}

---
---

Possible cause: method not found (e.g. upper lower case letter 
difference)

The 'main()' method can not be found by the java.exe interpreter.
(in the java file which you are currently compiling)

---
---

Possible cause: you should only give the class filename when 
interpreting with java.exe (and not include the path)

For example:

class Mytestclassname {
 public static void main( String args[] ) {
 }
}

---

Then interpreting it, using java.exe:

 java.exe c:\temp\Mytestclassname

gives errors.

But

 java.exe Mytestclassname

works OK.

---
---

Solution:

Define a main method in the java file which you are currently
compiling.

---
---

Steps: Overview:

 1. -Create the program

   1. Save this in the file 'Mytestclassname.java'

--- cut here: begin --------------------------------------------------

public class Mytestclassname {
 public static void main() {
  Myclassname myobjectO = new Myclassname();
  System.out.println( myobjectO.I );
 }
}

--- cut here: end ----------------------------------------------------

   2. Save this in the file 'Myclassname.java'

--- cut here: begin --------------------------------------------------

public class Myclassname {
 public int I = 10;
}

--- cut here: end ----------------------------------------------------


 2. -Run the program

+------------------------------------------------------------+
|[C:\TEMP]set classpath=c:\temp                              |
|                                                            |
|[C:\TEMP]c:\j2sdk1.4.1_03\bin\java Mytestclassname          |
|Exception in thread "main" java.lang.NoSuchMethodError: main|
|                                                            |
|[C:\TEMP]c:\j2sdk1.4.1_03\bin\javac Mytestclassname.java    |
|                                                            |
|[C:\TEMP]c:\j2sdk1.4.1_03\bin\java Mytestclassname          |
|Exception in thread "main" java.lang.NoSuchMethodError: main|
|                                                            |
|[C:\TEMP]c:\j2sdk1.4.1_03\bin\javac Mytestclassname.java    |
|                                                            |
|[C:\TEMP]c:\j2sdk1.4.1_03\bin\java Mytestclassname          |
|Exception in thread "main" java.lang.NoSuchMethodError: main|
+------------------------------------------------------------+

 3. The problem is thus that it does not find the expected
    default 'main' method, because here is used 'Main' instead,
    and because of Java is case sensitive, that is not the same.

///////////////////////////////////////////////////////////////

Cock Roach:

If what you have is an applet, pay attention to how you compile and
execute, it does not have a main() function to initialize it!

The Simple applet, like every other applet, features a subclass of the
Applet class. The Simple class overrides four Applet methods so that it
can respond to major events:

init

    To initialize the applet each time it is loaded (or reloaded).

start

    To start the applet's execution, such as when the applet's loaded
    or when the user revisits a page that contains the applet.

stop

    To stop the applet's execution, such as when the user leaves the
    applet's page or quits the browser.

destroy

    To perform a final cleanup in preparation for unloading.

Following is the interface for these methods:

public class Simple extends JApplet {
    . . .
    public void init() { . . . }
    public void start() { . . . }
    public void stop() { . . . }
    public void destroy() { . . . }
    . . .
}

----------------------------------------------------------------------