Skip to content

Extension incorrectly runs cached .class file from another class of same name with no package #1587

Description

@aatle

Bug report

Suppose there is a file structure:

folder1/
    Main.java
folder2/
    Main.java
// folder1/Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Main 1");
    }
}
// folder2/Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Main 2");
    }
}

No package statements; the projects are not connected at all.

When folder1/Main.java is run first, it gives the correct output Main 1.
But if folder2/Main.java is then run, it gives the wrong output of Main 1 instead of Main 2; the first built file is run instead.
folder1/Main.java 'shadows' folder2/Main.java, since the class name is the same and there are no package statements.
Another file structure that causes this is ./Main.java, ./folder/Main.java

This bug is inconvenient when multiple different, unrelated Java projects are in the same workspace.

Activity

  1. github-actions commented on Nov 23, 2025

    @github-actions

    Hi aatle, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.

    Workarounds and Suggestions

    • Declare an explicit package in each directory to avoid name collisions, e.g.

      // folder1/Main.java
      package folder1;
      public class Main { … }
      
      // folder2/Main.java
      package folder2;
      public class Main { … }

      (see Unable to determine the scope of the class redhat-developer/vscode-java#541 (comment))

    • Open each project in its own VS Code workspace, or whitelist only one source folder at a time via java.project.sourcePaths in your settings. This prevents the language server from loading both folders simultaneously.

    • Run the Java: Clean the Java Language Server Workspace command (F1) after switching between directories to flush cached .class files.

    Tracking

    This behavior is a known issue in the VS Code Java extension where classes with identical names in different folders share a cache. It is being tracked here:

    No fix is available yet to automatically isolate runs of same-named single-file programs.

    The team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions