Java Package Does Not Exist Error
Asked Answered
A

14

55

So there's a folder /usr/share/stuff in the root directory

in stuff there are a bunch of java files with package org.name definitions at the top

I am running javac test.java where test.java is in a subdomain

I added /usr/share/stuff to my class path.

and at the top of test.java I add import org.name

But I get a package does not exist error...why?

Aylmar answered 16/6, 2011 at 16:19 Comment(2)
Assuming you have compiled the java files into the correct directory (class files are in "/usr/share/stuff/org/name"), you also need to import the actual files or the wildcard into test.java ('import org.name.MyClass' or 'import org.name.*'). If you just do 'import org.name' this error happens (ask me how I know lol)Rhinencephalon
is this a Runtime exception or Compile time exception?Reid
H
34

Are they in the right subdirectories?

If you put /usr/share/stuff on the class path, files defined with package org.name should be in /usr/share/stuff/org/name.

EDIT: If you don't already know this, you should probably read this doc about understanding classpath.

EDIT 2: Sorry, I hadn't realised you were talking of Java source files in /usr/share/stuff. Not only they need to be in the appropriate sub-directory, but you need to compile them. The .java files don't need to be on the classpath, but on the source path. (The generated .class files need to be on the classpath.)

You might get away with compiling them if they're not under the right directory structure, but they should be, or it will generate warnings at least. The generated class files will be in the right subdirectories (wherever you've specified -d if you have).

You should use something like javac -sourcepath .:/usr/share/stuff test.java, assuming you've put the .java files that were under /usr/share/stuff under /usr/share/stuff/org/name (or whatever is appropriate according to their package names).

Highchair answered 16/6, 2011 at 16:21 Comment(1)
Thanks!...This still didn't work though...i've done this but it still says package cannot be found. Is it enough to edit the classpath on the subdomain or does this need to be edited at the root as well?Aylmar
A
38

Just reimport didn't work. Following worked for me.

File -> Invalidate Caches /Restart

Then

Build -> Rebuild Project

That will reimport maven project.

Acicular answered 1/6, 2021 at 8:27 Comment(1)
It was useful. Worked for me.Beret
H
34

Are they in the right subdirectories?

If you put /usr/share/stuff on the class path, files defined with package org.name should be in /usr/share/stuff/org/name.

EDIT: If you don't already know this, you should probably read this doc about understanding classpath.

EDIT 2: Sorry, I hadn't realised you were talking of Java source files in /usr/share/stuff. Not only they need to be in the appropriate sub-directory, but you need to compile them. The .java files don't need to be on the classpath, but on the source path. (The generated .class files need to be on the classpath.)

You might get away with compiling them if they're not under the right directory structure, but they should be, or it will generate warnings at least. The generated class files will be in the right subdirectories (wherever you've specified -d if you have).

You should use something like javac -sourcepath .:/usr/share/stuff test.java, assuming you've put the .java files that were under /usr/share/stuff under /usr/share/stuff/org/name (or whatever is appropriate according to their package names).

Highchair answered 16/6, 2011 at 16:21 Comment(1)
Thanks!...This still didn't work though...i've done this but it still says package cannot be found. Is it enough to edit the classpath on the subdomain or does this need to be edited at the root as well?Aylmar
H
4

I was having this problem, while trying to use a theme packaged as .jar in my app, it was working while debugging the app, but it didn't when building/exporting the app.

I solved it by unzipping the jar, and manually add its contents to my build folder, resulting in this:

project/
   │
   ├── build 
   │   └── classes
   │       ├── pt
   │       │   └── myAppName ... 
   │       └── com
   │           └── themeName ...
   ├── src
   └── lib

I don't have the error anymore and my app loads with the intended theme.

Housewarming answered 4/8, 2014 at 17:39 Comment(1)
This will work but is the wrong approach; you should instead change your settings to include the .jar inside your .jar when you export.Tatman
L
2

You need to have org/name dirs at /usr/share/stuff and place your org.name package sources at this dir.

Lianeliang answered 16/6, 2011 at 16:21 Comment(0)
M
2

I had the exact same problem when manually compiling through the command line, my solution was I didn't include the -sourcepath directory so that way all the subdirectory java files would be compiled too!

Miun answered 8/6, 2012 at 22:15 Comment(0)
H
2

Right click your maven project in bottom of the drop down list Maven >> reimport

it works for me for the missing dependancyies

Hornbook answered 28/8, 2018 at 4:20 Comment(0)
C
0

If you are facing this issue while using Kotlin and have

kotlin.incremental=true
kapt.incremental.apt=true

in the gradle.properties, then you need to remove this temporarily to fix the build.

After the successful build, you can again add these properties to speed up the build time while using Kotlin.

Catabolism answered 20/11, 2019 at 10:14 Comment(0)
S
0

You should add the following lines in your gradle build file (build.gradle)

dependencies { 
     compile files('/usr/share/stuff')
     ..
}
Seedcase answered 28/8, 2020 at 11:56 Comment(0)
H
0

If you have 2020 version, that you've most likely got a bug, like the one I got. Tried multiple things, but what helped me is a very simple thing. Settings -> Build, Execution, Deployment -> Build Tools -> Maven. Put a checkmark for Override of Local Repository, but leave the default value. Apply, OK. You can get an error, that's ok. Just press "Reload all Maven Projects" button (two rounded arrows, looks like regular Refresh button) in Maven tab on the right of the screen. Maybe clean and install. Should work after that.

Hotshot answered 15/7, 2021 at 12:28 Comment(0)
M
0

I came to this question because a bringing a file from one folder to another caused this error for me. The command that fixed it is below. I had the same package in multiple directories, and it was only looking at one of the directories.

So, if it's a Maven project, try mvn clean install --update-snapshots

Misplay answered 6/5, 2022 at 12:48 Comment(0)
R
0

I fixed it. In intellij idea:

  1. Go to project structure
  2. Select SDKs and make sure your JDK home path is the real path of your JDK
  3. Then use Maven clean install command

This solved my problem!

Relentless answered 9/5, 2023 at 9:43 Comment(0)
S
0

For me on intelliJ the solution was Right Click on project folder > Run Maven > Reimport Project

enter image description here

Sicular answered 30/5, 2023 at 6:52 Comment(0)
C
0

I had issue importing Kotlin class in Java class. Solution was to add kotlin-android plugin in module build.gradle as:

plugins {
    id 'com.android.library'
    id 'kotlin-android'         <- this
}

android {
    compileSdk 33
...
Charlsiecharlton answered 19/10, 2023 at 14:3 Comment(0)
H
0

I deleted folder "target" and this problem solved

Hachmin answered 15/1 at 13:37 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Blotch

© 2022 - 2024 — McMap. All rights reserved.