Click here to Skip to main content
15,938,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I included the main method in a simple java project :

public static void main(String[] args) {
       /* public void run() {

and it returned this error:
...\ class, interface, or enum expected

public static void main(String[] args) {<br />
              ^<br />
1 error

Process completed.

any idea what i am not getting right?
thanks, shasha
Updated 2-Dec-10 3:07am

1 solution

The method main has to be inside a class definition.
You'll need to wrap a class around that:

public class MyClass
    public static void main(String[] args)
        //do your stuff here

Since the error seems to be on line 82. You may want to check if you
somehow went past the class definition to place your main method.


Share this answer
Manfred Rudolf Bihy 3-Dec-10 2:59am    
Moved OP's "answer2 to comment:
thanx manfred,
but can i define another class and wrap it around that main method? and yes i think i went past the class definition and brought the main method to the end of the coding... or i just have to make sure i everything (variables and other methods) are within the main method?

public class computeNumbers extends JFrame implements ActionListener {

is my main class definition
Manfred Rudolf Bihy 3-Dec-10 3:07am    
It really depends what you're trying to do in that main program. Since it looks like you're writing some Swing application, I'd guess that you want to place the code that ramps up your GUI stuff inside that method.
For that I would use a separate class like and do all the stuff one needs to do to initialize the GUI.
shasha mamiya 3-Dec-10 5:42am    
hi, thanks a lot for your help, but i think since i am still new at this, i am getting a bit more uncertain; can you please take a look at this entire code again? i haven't added the compute methods yet, just want to get the GUI running and figure out the rest later... thanks again!

//edwin s. mamiya...
//short program to compute numbers...

import java.awt.*;
import java.awt.event.*; //java package for handling events such as user actions on buttons...
import javax.swing.*;
import java.util.*;
//import*; //if we are gonna work with file IO later with our little program, say saving to notepad...
//import java.sql; //...or database via ms access or (my)sql...

public class computeNumbers extends JFrame implements ActionListener {

public void JFrame(){

//declaring our labels, fields, etc...
JLabel label1 = new JLabel("First Number", JLabel.RIGHT);
JLabel label2 = new JLabel("Second Number", Jlabel.RIGHT);
JButton buttonCalculate = new JButton("Compute"); // the calculate button
buttonCalculate.setToolTipText("Click here to compute the numbers");
JButton buttonReset = new JButton("Reset"); // reset button...
buttonReset.setToolTipText("Click here to clear the fields");

JTextField numField1 = new JTextField(6); //this will only tolerate a 6 digit number input from the user...
JTextField numField2 = new JTextField(6); //same as above...

//numField1.setFocusTraversalKeysEnabled(false); //should we disable the TAB, SHIFT-TAB keys?
//numField2.setFocusTraversalKeysEnabled(false); //same as we did for numbField1 above?

JPanel contentPanel = new JPanel(); //introduce the main panel for the contents...
SCLayout scl = new SCLayout(2, SCLayout.FILL, SCLayout.FILL, 3); //the positioning of the contents...
scl.setScale(0, 1.5);

//now in the display panel we can add our content...
JPanel displayPanel = new JPanel();
SGLayout sgl = new SGLayout(3, 2, SGLayout.LEFT, SGLayout.CENTER, 5, 0); //the positioning of the displays on the content...
sgl.setColumnScale(0, 0.4);
sgl.setColumnAlignment(0, SGLayout.RIGHT, SGLayout.CENTER); //... and their alignments...
displayPanel.add(new JLabel ("First Number"));
displayPanel.add(new JLabel ("Second Number"));

//we create a panel to house our two buttons...
JPanel buttonPanel = new JPanel();
SRLayout srl = new SRLayout(3, SRLayout.FILL, SRLayout.CENTER, 10);
srl.setMargins(5, 15, 5, 15);

buttonCalculate.addActionListener(this);//this will listen to the calculate button...
buttonReset.addActionListener(this);//do for the reset...

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close the frame if user exits...
pack(); //pack everything on this panel...

public void actionPerformed(ActionEvent evt){
// Object obj = evt.getSource();
if (evt.getSource() == buttonCalculate){
//what should happen here if the user clicks on the Compute Button?
/**hence will include our method to compute the numbers entered by the user...*/

/** if (evt.getSource() == buttonReset){ //clear any entries made in the fields on Reset...
Reset rst = new Reset(); //??? is that really it?
reset(); //or is it this one?

public static void main(String[] args) {
/* public void run() {
Manfred Rudolf Bihy 3-Dec-10 5:56am    
You have to rework the part after public actionPerformed(ActionEvent evt). There seems to be a superflous closing parenthesis ")" at the end. Please use an editor supporting java syntax, that will help you to find matching parenthesis and stuff. (Notepad++ is good for that).
shasha mamiya 4-Dec-10 8:47am    
thanks again manfred! will see what comes out after this advice... good day!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900