• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

java.lang.NoSuchMethodError: main

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help in trying to understand why I am getting the following error message when i was once able to compile and run the program, but now all of a sudden, i'm getting en error.

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

java.lang.NoSuchMethodError: main
Exception in thread "main"


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

Below is the program code:



import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;

class Changes extends JFrame implements ChangeListener {

// Create the components
JSlider scale = new JSlider(0,100,10);
JLabel position = new JLabel("Set Position");

public Changes() {

// Create the window
super("Change Events"); setSize(300, 80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

// Create the container
Container content = getContentPane();
FlowLayout lay = new FlowLayout(FlowLayout.LEFT);
content.setLayout(lay);

// Configure slider component
scale.setMajorTickSpacing(10);
scale.setMinorTickSpacing(5);
scale.setPaintTicks(true);
scale.setPaintLabels(true);

// Add the event listener
scale.addChangeListener(this);

// Add the components
content.add(scale);
content.add(position);
setContentPane(content);
}
public void stateChanged(ChangeEvent event)
{
// Add the event handler
JSlider src = (JSlider) event.getSource();
if(!src.getValueIsAdjusting())
position.setText("Position is "+ scale.getValue());
}

public static void main(String[] args)
{ Changes eg = new Changes(); }
}


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


Thanks in advance!
 
lowercase baba
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you change?
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is Strange! main() looks good, but still showing this error.

try by changing you class definition from this




TO this


[ December 28, 2006: Message edited by: Prabhu Venkatachalam ]
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,

That's just it....I didn't change anything, which has me puzzled.


Prabhu,

I edited the code as you suggested and it still generates the same error.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to make a guess.

Since it's not a ClassNotFoundException, then the class can be found. But, the method isn't there. That tells me you have two or more classes with the same name and package structure on your classpath. The classloader is picking up the one without the method you want, not the one you want to run.

This is a classloader issue/classpath issue. I see these things all the time, especially when people 'creatively' package struts, cocoon and JSF applications.

Delete the class you are using, and see if the code still runs and gives the same error. If it does, the class is duplicated somewhere else on the file system. Get rid of that old class. Do a search or something to find possible duplications.

Classloaders are complicated things. Here's a link to a tutorial I wrote on J2EE classloaders. It is somewhat WebSphere specific, but the big picture concepts, especially the ones dealing with the bootstrap classloader and the SunMisc slassloaders all apply.

Why I Love Getting Loaded - ClassLoaded that is.

Happy Java!

-Cameron McKenzie
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron,

I understand what you are suggesting but I don't think that's the problem. I didn't have a problem compiling and executing programs before until yesterday. It's like all of a sudden my machine refuses to execute the code.

BTW, i did a search for the class file (as you suggested) and only found one on my HD.

If i had to guess, i'm thinking maybe something is not right with my CLASSPATH.
 
fred rosenberger
lowercase baba
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where does the .class file live, where are you calling java from, and what is your classpath?
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured it out. I went in under 'System' and edited the System variable under "Path". Everything now works like a champ.


Thanks guys!
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic