I have some problems with Visual Studio Code. I am using the Code Runner extension and Java for Multithreading and Multiprocessing training... but VS Code won't run my code.
Important: The name of the file is Main.java , the same of the public class Main
import java.util.*;
public class Main{
public static void main(String [] args) {
PrintThread t = new PrintThread("thread1");
t.start();
}
}
class PrintThread extends Thread {
private int sleepTime;
public PrintThread(String name) {
super(name);
sleepTime = (int) (Math.random() * 5001);
}
public void run() {
try {
System.err.println(getName() + " going to sleep for " + sleepTime + ".");
Thread.sleep(sleepTime);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.err.println(getName() + " done sleeping.");
}
}
This is the output:
tempCodeRunnerFile.java:5: error: class Main is public, should be declared in a file named Main.java public class Main{ ^ 1 error
I tried to change different names of file and public class (same name) but it doesn't work...