Eclipse doesn't see class/package in import - java

I use eclipse.
my problem description:
In import I see red underlyed strings. But if I navigate - I see source code.
How does it dispay?
How can I fix it?

You must adjust the project's build path, so it contains the missing packages.
Long answer
You may navigate every class that is known to the workspace. But if they are underlined it means they are not available when building the project.
Click on the node "Referenced Libraries" to see what's included. If your imported package JAR is not listed there, adjust the build path.

Right-click on the folder containing "com/epam/dentsply/service" and choose "Build Path/Use as Source Folder". Be sure to select the one directly containing the "com" directory with your java files.

Related

Why isn't intellij giving me the option to create new classes?

Here's what I'm seeing:
Normally I can choose "New Java Class" but for some reason I'm not getting this option. I created this project by opening an empty directory and building it out from there. I've seen this question, but that answer didn't help me. Here's my module settings:
Even if I open my pom.xml directly as a project, it still has this problem. How do I fix this?
The directory or one of the parent directories must be marked as Source Root (In this case, it appears in blue).
If this is not the case, right click your root source directory -> Mark As -> Source Root.
For some reason, intellij didn't recognize my directories as packages. If I created something directly from the java directory, it would give me the correct options. I fixed this by deleting everything under java/ and recreating it. This may be an intellij bug.
This happened when I was trying to add classes to a generated package using xjc plugin.
Intellij allows to add classes only to packages which are correctly names and have the "source root" status.
Can be fixed as-> right click on the directory where you're not able to add classes:
Mark sources root
refer image for clarity
This worked for me :
Right click the java folder (myproject/src/main/java)
Click "Mark Directory as"
Click "Source Root"
Right click the myproject folder (myproject/src/main/java)
Click "Mark Directory as"
Click "Source Root"
So basically had to mark some inner folder as Source project and alter mark parent project again as Source project..
Hacky way but this worked for me.

How to use a class from JAR in eclipse

I have two jar files - jar1 and jar2. Both of them are located in C:\Eclipse projects\ and I have added the paths to both of them to the Environment Variable CLASSPATH as follows
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Eclipse projects\stdlib.jar;C:\Eclipse projects\algs4.jar
the ".;" at the beginning were there so I left them. Then I added the jars to the project from their location C:\Eclipse projects\ and they showed up as Referenced Libraries. However, when I try to instantiate a class from the jars it does not recognize it. I am also not able to import the jar (import jar1).
After I tried adding a lib folder in the project and I added the jars there. After, I added them as references once again (so not they appear twice in the Referenced Libraries), however, I am still not able to use the inner classes. Any help will be much appreciated.
UPDATE:
Something must be wrong on my end. None of the suggestions worked for me. Here is a video with all the steps: screencast.com/t/gC81YzCsLY0e
RESOLUTION In my project I had a package called TestProject and it seems that those jars needed a default package. After deleting the TestProject package and using a defaultPackage everything worked correctly after adding external JARs as explained below.
I've got the same problem as you today, And no answer from the web can solve it. However, I fixed it at last.
In fact, there is nothing wrong with the setup, it is right to import those jars through "Add External JARs". The real problem is the location/package of you java code. I found that you have to put your .java file in the default package. For example, you will get errors if you put your java code in a package like com.xxx.yyy.ccc, below is an image which shows the right location/package you should use(see WTF.java). After doing that, you program will be able to run.
However, that is how i fixed my problem, i'm not sure that could work for everyone..
In eclipse, right click on a project->Propeties->Java Build Path->Add External JARs (Add JARs if the jar is inside the project's folder) and then choose your jar file.
From now you can use the inner classes of the jars you added. Eclipse will import them when you'll start using them.
Why don't you use these two JARs—— stdlib-package.jar and algs4-package.jar.
And below the code page(http://algs4.cs.princeton.edu/code/)
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar or algs4.jar. Why not?
A. The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use these package versions: stdlib-package.jar and algs4-package.jar.
Warning: if you are taking Princeton COS 226 or Coursera, Algorithms, Part I or II, you must use the default package verison of our libraries to facilitate grading.
Showing my test success:
If you have a folder with your JAR files into the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add JARs, search and select the JARs files you want to use.
If you have yours JAR files any other place outside the project:
Right click on the project>Build Path>Configure Build Path;
At the tab "Libraries" click on Add External JARs, search and select the JARs files you want to use.

How to import a Java program into Eclipse?

I have some java source code. It an just an archive which is four folders.
Folders MAIN, DATA, TAGS, USER_INTERFACE. Each folder contains a few class files.
I see how everything works together, but can't see to get the program to import correctly or run. I did find a MANIFEST.MF.
What is the proper way to import such a project?
Create an Eclipse project. Under the project settings, select the "Java Build Path", and add each source folder (and remove the default src folder that Eclipse may add for you).
If necessary, you may set the output/build folders for each source directory, and clear Eclipse's default bin output directory. Check the "Allow output folders for source folders" to enable this, then set each source folder's output folder.
(You may or may not want to keep compiled classes in individual directories.)
If there are class files in the project that aren't generated from source in the project, in the same dialog, select the "Libraries" tab, and click the "Add Class Folder" button to add dependencies from within the project.
It doesn't seem that your source code is an eclipse-importable project (it would have to have a .classpath and .project file in the top level directory for that).
You should instead create a new Java project. For simplicity, set the project's location to be the parent directory of your MAIN, DATA, etc. directories. Then after the project is created, configure a source folder for each of MAIN, DATA, etc. (right-click the project, choose Properties > Java Build Path, then work in the Source tab). Your source folders should then show up in the navigator and the project should be able to be compiled.
Create a hello world project in eclipse and understand the project structure in eclipse.
And then manually import the files it is simple and avoids a lot of confusion especially for somebody new to the environment.

How to take package .java files into a Eclipse Project

I've got a copy of a java package, with example implementation. The package is in a structure called com.java.project (folders, containing myriads of .java files) and there's also a few example files (example1.java, example2.java)
The example files have 'import com.java.project' calls in them, that's all well and good. However, I can't seem to work out how to get Eclipse finding the package contents (currently returns "Type not found" for any calls to the project objects.
I've tried placing the com structure in the same level as my example1.java, but that doesn't work. I have read that I need to compile the package into a .jar to get it to work w/Eclipse, but that doesn't seem to want to behave, none of the files compile.
In Eclipse you can right click on the Project Explorer and select "New -> Java Project"
Then you can import your java files by right clicking your project, selecting import from file system. If the class file shows an error, open the class file, the class name should have red curly braces, click on the error tick mark and Eclipse will give you an option to move the file to the correct package which in turn creates the folder structure for you.
1.Can you do a check on the build path of your Java project? (Right-click the project -> Properties -> Java Build Path -> Source tab), and see if the root folder contains the code is properly specified here.
2.Maybe a Project->Clean will help as well.
Hii,
Just Create a new project with File -> New menu
you need to open Java Perspective and there you will find a package named "src" you need to copy your "com" directory and paste it at the src folder.
I hope this will work for you.

Eclipse WTP: "The import ___ cannot be resolved for" error in a JSP file for a class defined in project

A Visual Studio user struggling w/ Eclipse...
I imported a set of servlets/JSPs into a project in Oracle Workshop for Weblogic. /page.jsp has the following import statement:
import="com.foo.bar.*"
Eclipse is displaying an error:
The import com cannot be resolved.
The class that implements the above import is in /WEB-INF/src but I don't know how to build the class in Eclipse to resolve that error. I assumed Eclipse would automagically build the .java file and place the output in /WEB-INF/classes, but it's not doing that.
It could be that I haven't structured my project directories correctly so perhaps that's why Eclipse isn't building my source. Any suggestions? How can I get this to work?
First of all, /WEB-INF/src is a rather strange place to keep your java sources; you may want to move them out of /WEB-INF (into /src in project root, for example)
Either way, you need to tell Eclipse where your sources are and where you want classes built to. It's done in project properties dialog:
Right-click on your project in Eclipse, select Properties
Click on Java Build path on the left
Click source tab on the right
Click Add Folder button and add your source folder (/WEB-INF/src or wherever you moved it to)
Ensure Allow output folders for source folders is checked below
Under newly added source path select output folder and point it to /WEB-INF/classes or other location of your choice.

Categories