Search code examples
javavisual-studio-codeimportpackageexists

I have created a folder/package. two subpackages (first one=subpkg1 and second one=subpkg2) a file inisde subpkg1and import file from subpkg2. and


enter image description here

I am using VS code for learning java. I have created a folder/package(name=pkg). Then I created two packages (first one=subpkg1 and second one=subpkg2) inisde the package. But I created a file (test1) inisde subpkg1 and import file (name= food) from subpkg2 then compile and run. I kept getting the error that pkg.subpkg2 doesn't exist (see the image)

I am expecting it to get the file imported. please answer it for vs code


Solution

  • The problem here is you only went to the "tr" package and compiled test.java. The restaurant.java file is neither compiled nor in the package.

    Try first cd to the "Package" (containing both "fil" and "tr"), then use this command

    javac -d {packageName} tr/test.java fil/restaurant.java
    

    to compile both files and put the .class files to the same package (the "-d" command does so). If the package name you entered doesn't exist, it should be created under the Package folder. After this, use the following command

    java -cp {packageName} Package.tr.test
    

    or other commands to run the code.