I have the following setup in my project:
com.foo
- Main
com.foo.util
- StringUtil
In Main I import the StringUtil using
package com.foo;
import com.foo.util.StringUtil;
And use it just as you would use it;
StringUtil string = new StringUtil();
Yet, eclipse keeps telling me that StringUtil cannot be resolved to a type - how can this be?
I already tried refreshing the project, reimporting it as a new project, clicked through the build paths but everything seems to be set up correctly.
But still, eclipse doesn't recognize the class and won't compile my project.
EDIT: The original question had this format:
package com.foo;
import com.foo.StringUtil;
Thus my answer was:
Because StringUtil is in com.foo.util and not in com.foo.
Change it to import com.foo.util.StringUtil.
Use CTRL + SHIFT + O shortcut to organize imports automatically. It will save your time.
Can be a typo, but in your import it says import com.foo.StringUtil;, but shoud be import com.foo.util.StringUtil; try press Ctrl-1 (Quick fix) when class is in focus. This gives suggestions on errors
Turns out it was due to some messed up eclipse installation.
Wiping the project, checkout it again and reimport it in a restarted eclipse solved every issue.
Is your class public (Which I believe it is if you can import it) with default constructor there if there are overriden constructor and otherwise no overridden constructor + constructor being public?
Copying the project to a new project solves this issue.
Related
I've just started a new IntelliJ project. When I try and import a java class with the IntelliJ import shortcut, the import is being added to the object and not to the top of the class like it normally would.
Where in the settings can I change this?
See Preferences/Settings > Editor > Code Style > Java if this option ...
Use fully qualified class names
... is ticked then IntelliJ will always use fully qualified class names. If you disable this option then Intellij will include the import statement and refer to the class by its 'simple' name.
Here's a screenshot showing this configuration item:
If the accepted answer is not working for anyone, Try the below points too.
Check whether the class file is in correct package and resolve if there is any issues in the class file.
Click File -> Invalidate Caches/ Restart and Click Invalidate and Restart button, Once it restarted U'll be able to import properly.
I was facing this issue with my custom / generated(protobuf) java packages.
Solution (TDLR)
Whenever you face this issue, just check the package or class that getting imported doesn't have any error(red marking).
If so try to fix that.
I was having an issue with package naming and its actual location. By generated-sources -> Right Click -> Mark Directory As -> Sources Root corrected my issue.
Check whether generated-sources or folder under the generated sources need to mark us Sources Root.
My issue was with generated java files (Adding this just to help someone else with same issue)
Intellij shows red mark for Import with wildchar(import pkg1.subpkg1.*;). Like editor was unable to recognizing 'import all' under a package.
But direct import to the inner package was fine.
On top of that an resolving issue with import class add full package to the Class itself, instead of adding import statement on top the java file.
This issue didn't affect the code compilation, only in editor it was showing red mark.
I want to use the StdDraw package, and I've tried many different ways of importing it.
Of course I tried:
import StdDraw;
But, when I look at the documentation from Princeton here, it shows that StdDraw is part of Objects, so I try this:
import java.lang.Object.StdDraw;
However, this results in an error:
error: cannot find symbol in
import java.lang.Object.StdDraw;
I saw this question here but it does not answer this question either.
How do I import StdDraw? thank you.
if you want to use StdDraw you must have
either the sources
or the classes (best zipped up as jar)
as preferred way you use the sources (see http://introcs.cs.princeton.edu/java/15inout/). it says there "To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program. "
once you did this the imports should be working.
NOTE: all four files are not in packages, so you should 'download' them into the 'standard' package. That means you have to download them to the root package of your project.
by the way: don't import import java.lang.Object.StdDraw; but do just import import StdDraw;
First of all check encoding of your IDE. It should be set to UTF-8. It is important if you are using MS Windows operating system.
Then create StdDraw.java class in the same package as the package of your program you are writing. Remove class declaration, leave only package declaration.
Then visit this page: https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java .
Copy all its contents (Ctr-A, Ctrl-C) and then paste it into StdDraw.java file you created previously.
StdDraw.java has its own main method so try to run it in order to check that the library works correctly. You should see a window with four strange figures :) .
Don't touch StdDraw.java anymore. Now you can easily import StdDraw library and refer to its methods with name of the class.
Enjoy
In my Xpage, I have added to Java classes. One is "AUser" declared in Models package and one is "AUserRepository" declared in a Repository package.
When I try and import AUser into a class in my AUserRepository... the import statement I type in is not working.
import com.Discussion.utils.AUser;
Weird thing is, If I exit out of Notes and load up eclipse standalone, and make the same kind of stuff, the import works fine. Am I missing out some important factor in Xpages?
The problem definitely is the import routing.
import Models.AUser;
import Repository.AUserRepository;
Is the project build path correct? Project - Properties - Java build path. Also, have you tried cleaning the project? Project - Clean....
The screenshot is the error I am receiving. I am trying to use the class ConnectDB contained within the package CIS4362Connect1, in the class AdminManager in the package myconnectoracle. Why can't I import this class?
As comments above indicate, it is possible that your ConnectDB is not public, in order to use that close outside of the its package, it must be declared public
Please refer to: Access control documentation
EDITED: (Solution)
The problem is that packages are case sensitive, you have a different case in both packages declaration.
inside the ConnectDB class you maybe have cis in lower case.
As far as I can tell, the class you are looking for is not defined in the current project. Try referencing the project with the package you are looking for in the libraries, or just move the package/class.
I just got a very similar issue with one of my class being unexplicably unimportable. I tried building the project anyways, and it did build successfuly, despite errors showing everywhere.
Moving the package to another location within my ide, and then moving it back to where it was supposed to be located somehow solved the issue for me.
The problem is that the Eclipse compiler do not see any layout or ids, or even strings and values in res folder, though R class is generated well (I've opened it, but of course didn't change enything). So I've tried to Clean and Build project and even restart Eclipse, compare the ids - the problem is still Eclipse don't see anything from res.
What should I do? May be someone solved this yet?
The SDK generates resource classes in the root package of your project. So, if your root package is com.project, and you're trying to use it from some other package (which is or is not directly under the root package), you need to import com.project.R.
i think importing import android.R; you can't see any ids or layouts or strings
change it in import com.companyname.projectname.R;
Get package name from android manifest file.and import like import packageName.R;