class bubbleSort1{


public static void bubbleSort(int[] x) {
int n = x.length;
for (int pass=1; pass < n; pass++) { // count how many times
// This next loop becomes shorter and shorter
for (int i=0; i < n-pass; i++) {
if (x > x[i+1]) {
// exchange elements
int temp = x; x = x[i+1]; x[i+1] = temp;
}
}
}
}
}
the output say..

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

Dani AI

Generated

Short answer: the JVM is reporting that it cannot find a proper program entry point (the main method) in the class you tried to run. and already pointed this out and supplied a working example that includes a main to call the sorter.

Things to check (common pitfalls that cause the same runtime error):

  • Method signature problems: wrong name case, missing static or wrong access/return/parameter types will not be recognised as the entry point.
  • Running the wrong class: command-line/classpath can pick up a different class file or JAR that has no main.
  • Package vs directory mismatch: a package declaration requires running the fully qualified class name from the correct top-level directory.
  • JAR execution: java -jar requires a Main-Class in the manifest; otherwise the JVM won’t find main.
  • Inner/nested classes or IDE run-configs: the class you think you launched may differ from the one the IDE runs.

Quick diagnostic steps and fixes (console commands are useful):

javac bubbleSort1.java
java bubbleSort1
javap -public bubbleSort1

Use javap to inspect the compiled class and confirm a proper main method exists. If the class is in a package, run it by its full package name and ensure the directory layout matches. If using a JAR, examine the manifest. Remove or rename any old class/JAR files that might be on the classpath so the JVM runs the newly compiled class.

Note: this is a runtime error from the JVM when it cannot locate a valid application entry point — compiling can succeed even when the correct main is absent. See ’s posted example for a full wrapper that reads input, sorts, and calls a main to run the program.

Recommended Answers

All 7 Replies

class bubbleSort1{


public static void bubbleSort(int[] x) {
int n = x.length;
for (int pass=1; pass < n; pass++) { // count how many times
// This next loop becomes shorter and shorter
for (int i=0; i < n-pass; i++) {
if (x > x[i+1]) {
// exchange elements
int temp = x; x = x[i+1]; x[i+1] = temp;
}
}
}
}
}
the output say..

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

The class that you're trying to run doesn't have an entry point (main) method.

Try declaring this method in your class--

public static void main(String[] args){


}

-- also please use code tags.

in your programeme no main function
so youcan compile it
bu u can't run that programme
see syntax

import java.io.*;
class bubbleSort1{
public int x[],max;


public void input()throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the Limit:");
max=Integer.parseInt(in.readLine());
x=new int[max];
for (int pass=0; pass <max; pass++)
{
System.out.print("Enter the value for "+pass+" element :" );
x[pass]=Integer.parseInt(in.readLine());
}
}


public void bubbleSort() {

for (int pass=0; pass < max; pass++) {
for (int i=1; i < max-pass; i++) {
if (x[i-1] > x) {
// exchange elements
int temp = x[i-1]; x[i-1] = x; x = temp;
}
}
}
}


void result()
{
for (int pass=0; pass < max; pass++)
{
System.out.println(x[pass]);
}
}


public static void main(String agr[])
{

bubbleSort1 obj= new bubbleSort1();
try
{
obj.input();
obj.bubbleSort();
obj.result();
}
catch(Exception e)
{System.out.println(e);}
}
}


In ur coding there is no main method thats wy that problem occured.

import java.io.*;
class bubbleSort1{
public int x[],max;


public void input()throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the Limit:");
max=Integer.parseInt(in.readLine());
x=new int[max];
for (int pass=0; pass <max; pass++)
{
System.out.print("Enter the value for "+pass+" element :" );
x[pass]=Integer.parseInt(in.readLine());
}
}


public void bubbleSort() {

for (int pass=0; pass < max; pass++) {
for (int i=1; i < max-pass; i++) {
if (x[i-1] > x) {
// exchange elements
int temp = x[i-1]; x[i-1] = x; x = temp;
}
}
}
}


void result()
{
for (int pass=0; pass < max; pass++)
{
System.out.println(x[pass]);
}
}


public static void main(String agr[])
{

bubbleSort1 obj= new bubbleSort1();
try
{
obj.input();
obj.bubbleSort();
obj.result();
}
catch(Exception e)
{System.out.println(e);}
}
}


In ur coding there is no main method thats wy that problem occurred.

congrats
there is no error
its succesfully running

oh! very nice that your programme is properly running.what did happen to this.

commented: Do not post useless comments just to spread your sig links around. -2

put main method in your code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.