Search code examples
javavisual-studio-codeclassnotfoundexceptionjava.lang.class

Newly created or modified java files not running in vscode


I have a folder named java where i keep all the .java files
Recently I created a new java file to solve a simple DSA problem named MinimumTime.java

public class MinimumTime {
    public static void main(String[] args) {
        MinimumTime ob1 = new MinimumTime();
        int a[][]={
            {1,1},
            {3,4},
            {-1,0}
        };
        System.out.println(ob1.minTimeToVisitAllPoints(a));
    }
    public int minTimeToVisitAllPoints(int[][] points) {
        int n=points.length;
        if(n==1){
            return 0;
        }
        int time=0;
        for (int i = 1; i < points.length; i++) {
            int xDiff = Math.abs(points[i][0] - points[i - 1][0]);
            int yDiff = Math.abs(points[i][1] - points[i - 1][1]);
            time += Math.max(xDiff, yDiff);
        }
        return time;
    }
}

This is the code but when i click the Run button on the top right corner i get this error in the terminal Error: Could not find or load main class MinimumTime Caused by: java.lang.ClassNotFoundException: MinimumTime I thought it was problem with the jdk so to check it i tried the javac MinimumTime.java and java MinimumTime both were working perfectly fine it was creating a new .class file and executing the code as expected so i tried running other java files in the same folder they were working as expected so i tried modifying the code in an existing file and it was not working anymore and was giving the same error

i tried running other java files in the same folder they were working as expected so i tried modifying the code in an existing file and it was not working anymore and was giving the same error

Edit: I just created an new .java file named Test.java in that i intentionally named the class with the main fucntion as class MinimumTime it was giving the error Error: Could not find or load main class MinimumTime Caused by: java.lang.ClassNotFoundException: MinimumTime in stead of Error: Could not find or load main class Test Caused by: java.lang.ClassNotFoundException: Test


Solution

  • There is no problem with the code in the problem. Follow the following steps

    1. New script, named MinimumTime.java

      enter image description here

    2. Paste the code in the post

    3. Click the RUN execution code above the main method

      enter image description here

    If you have been wrong, try Ctrl+Shift+P --> Java: Clean Java Language Server Workspace and restart vscode.