skip to Main Content

When I create a java package inside the src folder in the java project I am working on, without using any package, everything works just fine, just when I create that package folder and move all my java files to it though all the files have the package name correct in them and everything else is correct, but I get these 2 errors below, i will provide you with some screenshots to better understand the situation.

Main.java:6: error: cannot find symbol
            Circle2D c1 = new Circle2D(2,2,5.5);
            ^
  symbol:   class Circle2D
  location: class Main
Main.java:6: error: cannot find symbol
            Circle2D c1 = new Circle2D(2,2,5.5);
                              ^
  symbol:   class Circle2D
  location: class Main
Main.java:7: error: cannot find symbol
            System.out.println(c1.getArea()+" "+ c1.getPerimeter()+" "+c1.contains(3,3)+" "+c1.contains(new Circle2D(4, 5, 10.5)));
                                                                                                            ^
  symbol:   class Circle2D
  location: class Main
Main.java:8: error: cannot find symbol
            System.out.println(c1.overlaps(new Circle2D(3,5,2.3)));
                                               ^
  symbol:   class Circle2D
  location: class Main
4 errors

1

2

3

4

The only thing that worked is instead of running the code with "Run Code" button of the code runner extension I used "Run Java" but I don’t like to use it because it prints so much clutter and using the code runner features gives a clean output.

2

Answers


  1. it seems that your classes are trying to find each other in the current directory.

    To "route" them in the right way:

    1. Create a new directory in your project folder and call it .vscode. Visual Studio Code will orient on files in it when decides where to look for your .java files.
    2. Create settings.json file in Project1.vscode.
    3. Fill settings.json with the following text:
    {
        "java.project.sourcePaths": ["src"],
        "java.project.outputPath": "bin",
        "java.project.referencedLibraries": [
            "lib/**/*.jar"
        ]
    }
    

    You’re supposed to get result like this.

    If it stil does not work go to settings (Ctrl+,), type "launch" and get a single option. Selecting it will send you to global settings file, where you should find "configurations": [] and fill [] with the following:

        {
            "name": "Java",
            "type": "java",
            "request": "launch",
            "args": [
                ""
            ],
            "stopOnEntry": true,
            "jdkPath": "${env:JAVA_HOME}/bin",
            "cwd": "${fileDirname}",
            "startupClass": "${fileBasenameNoExtension}",
            "classpath": [
                ".",
                "${fileDirname}"
            ]
        }
    
    Login or Signup to reply.
  2. The only thing that worked is instead of running the code with "Run Code" button of the code runner extension I used "Run Java"

    You should use the official Extension Pack for Java and use it to execute your java project (Run Java). If you use other extensions that cause errors, it has nothing to do with Java.

    Code Runner is only used for executing code, it doesn’t have much advantage for complete projects, please use official extensions.

    https://code.visualstudio.com/docs/java/java-tutorial

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search