Search code examples
javanoclassdeffounderror

This is my code and i tried to find what the error but i couldn't find


1-Error: Could not find or load main class BasicSorting_1 2-Caused by: java.lang.NoClassDefFoundError: 3- Sorting/BasicSorting_1 (wrong name: BasicSorting_1)// enter image description here

package Sorting;

import java.util.*;

public class BasicSorting_1 {
    public static void bubbleSort(int num[]) {
        int n = num.length;
        for (int turn = 0; turn < n - 1; turn++) {
            for (int j = 0; j < n - 1; j++) {
                if (num[j] > num[j + 1]) {
                    // swap
                    int temp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                }
            }
        }
    }

    public static void display(int num[]) {
        int n = num.length;
        for (int i = 0; i < n; i++) {
            System.out.print(num[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int num[] = { 2, 3, 1, 7, 5 };
        bubbleSort(num);
        display(num);
    }
}

Solution

  • Maybe, you are running the 'project' instead of just the class BasicSorting_1. Then this error may apear because you have the "main method" in both classes (BasicSorting and BasicSorting_1), then Java can't determine wich class he will use as 'main'.

    Try running just the single java file;

    If that's not working, try checking out the package(if you really have it):

    package Sorting;