vscode-java-debug icon indicating copy to clipboard operation
vscode-java-debug copied to clipboard

VSCode and folders in repository from GitHub

Open espenj90 opened this issue 5 years ago • 8 comments

First of all I'm not an expert, but I will try to explain my issue as well as I can. I'm not sure if it's a bug, but I still need help to figure it out. I was working with a GitHub-repository with standalone .java-files (no Java-project, Maven etc.). I'm a student, and so far we are programming Java by making different classes in different .java-files, that works together by being in the same folder. Then I changed the name of the repository, and added some folders within the repository and moved my .java-files into one of those folders. Then I tried to run the my program, by hitting "run" on in the file that contain my "static void main-method". It then returned a "fileNotFoundException", because the program I coded was trying to read from a .txt-file inside the folder. The program had worked before I renamed the repository and moved the files into the folder. After some googling, I found out that the issue was that I moved the .txt file, and that this could be fixed by deleting the directory "Library/Application Support/Code/User/workspaceStorage". So I did, and it didn't fix the problem, and created another problem, which is now the problem that I need help with. When I open my GitHub-repository (which I have cloned to my Mac) in VSCode it opens fine, and I can open the folders inside the repository in the file-explorer in VSCode. But when I open one of the .java-files within one of those folders, it seems like VSCode sets this folder to a main folder or something, because when I go into one of those other folders inside the repository, and open one of the .java files there, then VSCode says that "filename.java is not on the classpath of project nameOfProject, only syntax errors are reported". But I have not created a project or anything, I just have .java-files inside the same folder, and they work together that way. So how do I fix this issue, because I didn't have a problem with this before, and it reported all sort of errors, not only syntax error

Environment
  • Operating System: MacOS 10.15.4
  • JDK version: 1.8.0_231
  • Visual Studio Code version: 1.43.2
  • Java extension version: 0.8.1
  • Java Debugger extension version: 0.25.1
Steps To Reproduce
  1. Open my GitHub-repository that has been cloned to my computer.
  2. Open a .java-file within a subfolder in the repository.
  3. Open a .java-file inside another of the subfolders in the repository.
  4. Try to run a file with static void main-method in it.

[attach a sample project reproducing the error] [attach logs] I try to add a logg here, hope this is the correct one

Current Result
Expected Result
Additional Informations

espenj90 avatar Mar 30 '20 09:03 espenj90

Go to File Explorer, try the context menu "Add Folder to Java Source Path" on the folder with "not on the classpath" warning. This action will mark the unrecognized folder as a source folder.

testforstephen avatar Mar 30 '20 10:03 testforstephen

Thank you for the reply, but I have already tried this, and this workaround creates another problem. If I have two files called Test.java (as an example) which both contains a class named Test, in two separate folders, and I add both folders to Java Source Path, then I get an error which says that "The type Test is already defined". Any other ideas of what I could try?

espenj90 avatar Mar 30 '20 10:03 espenj90

I don't think this is the correct approach to define two classes with the same name in the same namespace. This is not allowed by Java language. You must put them in different packages.

See a sample structure below, then you just need mark the src folder to Java source path.

- src
  - packageA
     - Test.java  // declare "package packageA;" in the header
  - packageB
     - Test.java // declare "package packageB;" in the header

testforstephen avatar Mar 30 '20 14:03 testforstephen

Yes, it might be that I'm doing something wrong, I don't really doubt that. I can try to explain myself a little better, so perhaps it's easier to figure out if it's possible to find a fix for me, or not. So my GitHub repository is called "IN1010", and inside that repository I have folders named Oblig4, Oblig5, Oblig6. In VSCode I just open my repository (IN1010), and then in the file explorer I can se all the folders (oblig4, oblig5, oblig6). Inside those folders I have several .java-files, which contains a lot of classes and together form a program inside that one folder. So you could say that I have one complete program inside each folder, and the programs don't have anything to do with the programs in the other folders. Every .java-file start like this:

class Classname {
}

So I don't have any project, package, src or anything. Does that help in understanding my problem, and to understand if there is a fix that I could try? Because then if I have a Example.java-file (with class Example{} inside it) in both oblig4 and oblig5-folder, I get the error like I told you about ("The type Example is already defined"). This is even if they are in separate folders, and have nothing to do with each other.

espenj90 avatar Mar 30 '20 18:03 espenj90

Impossible.

From the perspective of the build tool, what most concerned is about classpath. project, package, src are all used to help define the classpath. You didn't declare any packages, that doesn't mean there are no packages, but all classes are placed at the default package space. Name conflicting is not allowed at the same package.

When you opened multiple folders in the same VS Code window, then the classes in these folders are visible to each other. Either use different packages to isolate classes of the same name, or just open a subfolder at a time.

testforstephen avatar Mar 31 '20 01:03 testforstephen

You may take a look at multi-root workspace. https://code.visualstudio.com/docs/editor/multi-root-workspaces. Root will be considered as a kind of isolation mechanism. Classes with same name in different root are allowed.

testforstephen avatar Mar 31 '20 02:03 testforstephen

Ok. Thank you for the suggestion, I will try that.

espenj90 avatar Mar 31 '20 09:03 espenj90

Hi @espenj90, 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.

Suggestions to get all your Java files on the same classpath:

  • Always open the root repository folder (File → Open Folder) rather than individual subfolders or files. VS Code’s Java support treats each folder you open as a separate project.
  • If you must work with multiple subfolders, use a multi-root workspace (File → Add Folder to Workspace) so the language server sees all your sources together.
  • You can explicitly tell the Java extension where to find your sources by adding to your settings.json:
    "java.project.sourcePaths": [
      "folderA",
      "folderB"
    ]
    
  • Create a .vscode/launch.json to specify which Main class to run. Example:
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "java",
          "name": "Launch Main",
          "request": "launch",
          "mainClass": "com.example.Main"
        }
      ]
    }
    
  • For a full overview of Java project setup in VS Code, see the docs: • https://code.visualstudio.com/docs/java/java-project
Other references with low confidence
  • IllegalArgumentException: Path must include project and resource name
    Error when an Eclipse .project file makes the folder unloadable; unrelated unless you have .project files in your repo.
    https://github.com/redhat-developer/vscode-java/issues/1447

  • Can’t debug current file, always debug last Main class in workspace
    A debug-configuration issue where VS Code picks the wrong Main; relevant if you later add launch configs.
    https://github.com/microsoft/vscode-java-debug/issues/1337

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!

github-actions[bot] avatar Nov 11 '25 11:11 github-actions[bot]