Standalone Java files are not linked to default project unless being opened. Need to scaffold a project without Maven/Gradle
See original GitHub issueAs far as I know, currently whenever a standalone Java file is opened in VS Code, it is linked into “src” folder under the default project “jdt.ls-java-project”. So it allows users to start from scratch without importing a project.
But the feature doesn’t work for some scenarios. If I open a folder with some existing Java files, the files will not be linked to the default project unless I open each of one by one. Mentioned by @cjbailey in https://github.com/Microsoft/vscode-java-debug/issues/344#issuecomment-416157868
A simplified example:
There is a “Hello.java” file existing in my file system, and its’ content is:
/**
* Hello
*/
public class Hello {
public static void sayHello() {
System.out.println("hello");
}
}
I wanted to write simple Java code from scratch to test this Hello.java, so I created a “new folder” as my project root and a “src” folder to put java files, and copy “Hello.java” to the folder. Then I open this “new folder” with VSCode. Without opening “Hello.java” (this is important), I created a “Main.java”, referencing a method in Hello and press F5 to run, error occurred:
Some thoughts
-
If it supports to create a project from current workspace root folder, it would be a better experience for users to start from scratch. For a well-structured folder, I think it also makes more sense to create a project for it instead of link all the Java files into an invisible default project. In Eclipse, the “Import Projects from File System” just works fine for the case, I’m not sure if it is possible to have this feature here.
-
Moreover, a potential feature (https://github.com/redhat-developer/vscode-java/issues/619) enables user to set the source folder, in case language server fails to recognize source folder correctly.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
@michael-hawker The parent folder represents the direct folder containing the open file. Is this same as the current folder you mentioned? right?
One idea is when the user opens a standalone java file, prompt the user whether to add the parent directory as Java Source Folder, this will make all standalone java files under the same directory to be added to the default project classpath.
When the user wants to cancel the parent directory as Java Source Folder, he/she can do that through the context menu
Unmark as Java Source Folder
.In this approach, the user doesn’t need create a new eclipse project and get bothered by the generated .classpath and .project files, but directly leverage the default project to manage the standalone java files.