what does it mean when while trying to run a .class file I get "Exception in thread "main" java.lang..NoSuchMethodError: main"???
maelthra
- 5 Contributors
- forum9 Replies
- 10 Views
- 10 Years Discussion Span
- comment Latest Post by mailmeatvishnu
rinoa04 2
Most probably the .class file that you are trying to run is not a driver class where the main method is coded to call and run the coding.
maelthra
Well I am using the file from another post here in the forums, I was playing around look for code to go through to better my understanding.
the file is attached below, it compiles fine but won't run, and I get the afore mentioned error.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.Applet;
public class JMath extends Applet implements ActionListener
{
int Number1;
int Number2;
int ActualAnswer;
int UserAnswer;
int WrongCount;
int ccnt;
int totalTries;
Font headlineFont = new Font ("Helvetica", Font.BOLD, 38);
Label MathQuestion = new Label("xxxxxxxxxxxxxxxxxxxx");
TextField MathAnswer = new TextField (2);
// left and right in the assignment should be the same class
Button CheckAnswer = new Button (" Check Answer ");
String us = "__________________"; // to reserve room for label
Label MyAnswer = new Label (us);
Button AnotherGame = new Button("Another Game");
public void init () {
setFont (headlineFont);
add (new Label("MATH FUN"));
add (MathQuestion);
add (MathAnswer);
MathAnswer.addActionListener(this);
add (CheckAnswer);
CheckAnswer.addActionListener(this);
add (MyAnswer);
add (AnotherGame);
AnotherGame.addActionListener(this);
play(0);
}
public void play(int type) {
if (type == 0) {
Number1 = (int)( Math.random () * 9 +1);
Number2 = (int)( Math.random () * 9 +1);
ActualAnswer = Number1 * Number2;
WrongCount = 0;
String s = "What is " + Number1 + " TIMES " + Number2 + " ? ";
MathQuestion.setText(s);
}
MathAnswer.setText("");
MathAnswer.requestFocus();
}
public void actionPerformed (ActionEvent e) { // openaction
Object src = e.getSource();
int type = 0;
if (src == MathAnswer || src == CheckAnswer) {
try {
String a = MathAnswer.getText();
if (a.length() == 0) a = "**"; // to force an exception..
UserAnswer = Integer.parseInt(a); // make an integer from string
}
catch(NumberFormatException nfe) {
MyAnswer.setText("Wrong input !!!");
type = 2;
}
if (type != 2) {
totalTries++;
if (UserAnswer == ActualAnswer) {
ccnt++;
// always say '==' for a comparison, just '=' means assignment!
//MyAnswer.setText("Correct ("+ccnt+" out of "+totalTries+")");
int percent = (int)(100f * ccnt / totalTries);
MyAnswer.setText("Correct ( "+percent+" % )");
}
else {
WrongCount++;
MyAnswer.setText(""+WrongCount+" Wrong, Try Again");
type = 1;
}
}
}
else if (src == AnotherGame) {
MyAnswer.setText(us);
ccnt = totalTries = 0;
}
play(type);
}
}
bugmenot 25
it appears to be an applet. you usually have to embed the applet in an HTML page using applet tags and use a browser to run it
rinoa04 2
To run an applet, you will need an html file with this example coding:
<Html>
<Head>
<Title>Java Example</Title>
</Head>
<Body>
This is my page<br>
Below you see an applet<br>
<br>
<Applet Code="MyApplet.class" width=200 Height=100>
</Applet>
</Body>
</Html>
You must specify the .class file that you compile at the applet code tag. Use appletviewer command to run the html file on DOS.
darkagn
commented:
Very nice, concise explanation +1
maelthra
okay I played with the code did something different, so that I don't have to use, html. but I am getting all of these errors file is attached. if anyone could help me I would be very happy... LOL
F:\Javawork\Multiplication\Multiplication.java:22: <identifier> expected
System.out.println("How much is "); int = Number1 (" TIMES "); int = Number2 (" ? ");
^
F:\Javawork\Multiplication\Multiplication.java:22: illegal start of type
System.out.println("How much is "); int = Number1 (" TIMES "); int = Number2 (" ? ");
^
F:\Javawork\Multiplication\Multiplication.java:22: <identifier> expected
System.out.println("How much is "); int = Number1 (" TIMES "); int = Number2 (" ? ");
^
F:\Javawork\Multiplication\Multiplication.java:22: <identifier> expected
System.out.println("How much is "); int = Number1 (" TIMES "); int = Number2 (" ? ");
^
F:\Javawork\Multiplication\Multiplication.java:23: <identifier> expected
UserAnswer = input.nextInt();
^
F:\Javawork\Multiplication\Multiplication.java:26: illegal start of type
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
^
F:\Javawork\Multiplication\Multiplication.java:26: <identifier> expected
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
^
F:\Javawork\Multiplication\Multiplication.java:26: ';' expected
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
^
F:\Javawork\Multiplication\Multiplication.java:26: illegal start of type
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
^
F:\Javawork\Multiplication\Multiplication.java:26: <identifier> expected
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
^
F:\Javawork\Multiplication\Multiplication.java:26: ';' expected
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
^
F:\Javawork\Multiplication\Multiplication.java:30: illegal start of type
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
^
F:\Javawork\Multiplication\Multiplication.java:30: <identifier> expected
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
^
F:\Javawork\Multiplication\Multiplication.java:30: ';' expected
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
^
F:\Javawork\Multiplication\Multiplication.java:30: illegal start of type
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
^
F:\Javawork\Multiplication\Multiplication.java:30: <identifier> expected
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
^
F:\Javawork\Multiplication\Multiplication.java:30: ';' expected
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
^
17 errors
Tool completed with exit code 1
// Wayne Abbott
// 03/13/08
// Infinite Loop
public class Multiplication {
public static void main (String[] args)
{
int Number1 = 0;
int Number2 = 0;
int ActualAnswer = 0;
int UserAnwer;
Number1 = (int)(Math.random() * 9 +1);
Number2 = (int)(Math.random() * 9 +1);
ActualAnswer = Number1 * Number2;
}
// get answer from user
Scanner input = new Scanner (System.in);
System.out.println("How much is "); int = Number1 (" TIMES "); int = Number2 (" ? ");
UserAnswer = input.nextInt();
// check answer
if ( int = UserAnswer == int = ActualAnswer ) // if answer is correct
{
System.out.printf( "Very Good!");
}
if ( int = UserAnswer != int = ActualAnswer ) // if answer is not correct
{
System.out.printf( "No. Please try again.");
} // end
}
jwenting 1,649
do you know any Java at all?
Those lines are utter nonsense in the light of the language specification.
bugmenot 25
um.. so for starters, you know that you can only put code in methods? you can't have statements randomly outside methods
maelthra
okay this program is almost halfway done, right now I have no errors and the file runs fine, except I need it to ask another random question if the user gets the right answer, or back to the same question if the user gets it wrong. I am thinking of using if-else or while statements, but I am not sure how to form them. attached is the current file as is.
// Wayne Abbott
// 03/13/08
// Multiplication
import java.util.Scanner;
public class Multiplication {
public static void main (String[] args)
{
int number1 = 0;
int number2 = 0;
int actualAnswer = 0;
int userAnswer;
number1 = (int)(Math.random() * 9 +1);
number2 = (int)(Math.random() * 9 +1);
actualAnswer = number1 * number2;
// get answer from user
Scanner input = new Scanner (System.in);
{
System.out.println("How much is " + number1 + " TIMES " + number2 + "?");
}
userAnswer = input.nextInt();
// check answer
if ( userAnswer == actualAnswer ) // if answer is correct
{
System.out.printf( "Very Good!");
}
if ( userAnswer != actualAnswer ) // if answer is not correct
{
System.out.printf( "No. Please try again.");
}
} // end
}
mailmeatvishnu
check wether u had saved the name of file as a class which has got main method....remember it is case sensitive