2

I have been trying to execute this code in VS Code

    class test1 extends Thread
    {
       public void run()
       {
           for(int i=0;i<10;i++)
           System.out.println("Task 1");
       }


    }
    class task2 extends Thread
    {
      public void run()
      {
          for(int i=0;i<10;i++)
          System.out.println("Task 2");
      }
    }
    public class app
    {
         public static void main(final String[] args) throws InterruptedException {
             final test1 t1 = new test1();
             final task2 t2 = new task2();
            t1.start();
            t2.start();
        }
    } 

but it shows an exception in main thread:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

        at app.main(test1.java:21)

(see screenshot)

I tried different IDEs(like BlueJ) and this code is working fine with them. So what exactly is the problem I am facing while executing it in VS Code?

4
  • 2
    You will need to tell us what the error is. Show us the exception message and the stacktrace. Commented Apr 24, 2020 at 9:16
  • I have added a screenshot of that error message. Please, have a look at it. Commented Apr 24, 2020 at 16:29
  • OK. So it says there is a compilation error. What is the compilation error message? Commented Apr 25, 2020 at 4:27
  • Please post text, not links to images of text. Commented Apr 26, 2020 at 0:37

1 Answer 1

1

It looks like you’re having a file with a name test1.java, but the java compiler requires that the file name matches the name of the public class inside.

Try either

  • renaming the file to app.java
  • renaming the class apptest1, but then you’ll have to rename existing test1 to something else
  • make the app not public (and you will still be able to run it)

(I’m not sure why VS Code doesn’t report this problem properly.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, man! It is indeed true that the compiler requires file name to be matched with the name of public class inside, so just removing the public keyword makes my program to run without any error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.