Java refuses to see that package exists

Hey guys, I’m having some issues with trying to even start a Java project. I know I’ve created Java projects in Android Studio before, but there seems to be some really strange issues going on with why Java won’t recognize my packages I download from the internet.

So I go and create a completely clean new Java project in Intellij. I choose Java as the language and then choose Intellij as the build system. At this point, I’ve already tried all 3 build systems; Intellij, Maven, and Gradle and all 3 have this same issue I’m going to explain. I also choose JDK 18 since that’s the latest one I’ve downloaded.

After the project is successfully created, I go and create my first file under the src folder. I name it something like HelloWorld and I see HelloWorld.java in the folder list. I then download this Java file I got from Github and place it in my src folder. Then I do a import com.github.mrebhan.crogamp.cli.TableList; in my HelloWorld.java file. I resolve the issue with the Github Java file not being in the right spot by moving it appropriately using the options the light bulb gives me.

Next I instantiate the class and assign a variable to it and call the methods from that class. Next I do a Build → Build Project. Project successfully builds with no errors. Open the terminal in Intellij and cd to that src folder. Then I run java HelloWorld.java and then I get these errors

HelloWorld.java:6: error: package com.github.mrebhan.crogamp.cli does not exist
import com.github.mrebhan.crogamp.cli.TableList;
                                     ^

Even though the package exists in my project, it still continues to throw me this error. This isn’t just the only library it does this to either. So I get the package __ does not exist error even when installing packages from the Maven library that’s default to Intellij.

I’ve already done a File → Invalidate Cache and then rebuilding the project after Intellij restarts. That didn’t work. I’ve also tried installing Intellij on Windows (I’m currently using macOS as my main driver) machine and doing the same steps thinking maybe it’s a macOS issue and I still get the same error.

So does anyone know why this is happening and how I can fix this? I’ve tried every single possible way to just “include” an external file in my project.


EDIT: For the Maven libraries (repositories), I do File → Project Structure → Libraries on the left side → + sign to add a new library → From Maven (from that dropdown menu) → search for the library I want → Select only Transitive dependencies since that was the default (I’ve already tried every other checkboxes as well) → Click Ok → Choose my project the library will be downloaded to → Click Ok → Click Apply → Click Ok

Then I do the whole import blah.package.blah;, instantiate the class, select the method from that class, and then rebuild project with no errors and run java HelloWorld.java in the terminal again and still I get the package __ does not exist error even on the new Maven library that gets added instead of downloading files from Github and importing it that way.

Do you happen to see if it creates a folder called “com” in your project directory? In that should be a folder called “github” and inside that “mrebhan” etc. See how the folder structure matches the package names?

It looks like you are attempting to just rip this class out of some other project. In that case you can get rid of, or change, the package name at the top to reflect a folder structure in your project. If you want to put the class right in the same folder as your HelloWorld.java file, then you could just drop the package statement at the top altogether and just import it in HelloWorld.java as being in the same directory.

But code in packages typically follow a folder structure that matches the package name. You could check out something like this article…

And read up on how packages are made. You will want to pay attention to the second section on user defined packages and notice the screenshots how “university” and “department” are creating folders in the project.

Edit: Even notice how the source you are trying to use has its file in subfolders on the repository.

I’ve done everything possible at this point. So yes, when you use the light bulb to correct the folder path, it does actually move the file into that folder. So I do see “com”, “github”, “mrebhan”, etc. I’ve also tried to get rid of the package name as well and just have that file sit in the src folder along with my HelloWorld file. When I do that, I get the whole “cannot find symbol” error. That’s due to the fact the class isn’t being imported even though it is. The only “workaround” I was able to successfully do is say to heck with it and took the entire contents of that Github file and then remapped it to use my HelloWorld constructor and put it all in my HelloWorld file. That does actually work, but that’s not ideally what I wanted. Especially if I’m trying to import other libraries from other sources like Maven.

When naming the project, I have already named the nomenclature appropriately. So com.whatever at the start of creating the project.

Update: It turns out, it could be the terminal configurations that’s the problem. When I run the main method (in the output console) in my main file, it runs perfectly fine along with the imported libraries. I don’t get those errors and my program runs like normal. I’ve also ran it in debug mode and everything works as expected. Exporting the artifact to a Jar file also seems to work fine and the Jar file runs like it should. I was pulling my hair out because this whole thing didn’t make sense when everything was done correctly and this took me the entire day to figure out.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.