The import org.xxx cannot be resolved - java

I'm new to Java, so bear with me...
I have placed a jfreechart-1.5.0.jar file in a lib folder and wanted to do do some imports like this:
import org.jfree.chart.ChartFactory;
But VS Code is complaining: the import org.jfree cannot be resolved.
One thing I have to mention is, that I have created a copy of the project folder in order to try something. Because it worked, I have deleted the original folder and renamed the copy to the name of the original folder.
But now I have the problem that the imports do not work anymore, even it's the same code. I have "cleaned Java language server workspace" but it didn't help.
Is there anything else I have to do? Is the jar import pointing to a certain path / file?

For Maven project, you can add a dependency by clicking the + icon next to Maven Dependencies node in project view.
If your project is an unmanaged folder without any build tools. You can manage the dependencies by clicking the + icon or the - icon on the Referenced Libraries node or the items under it.

I had to reference the jar file to the project.
Surprisingly the first time it has been referenced automatically, the second time I had to do it manually...

Related

Java import error - org.jdom

I am attempting to edit code from an old developer.
I know it is bad practice but what she said I had to do was create a dummy project and then copy paste the packages in question modify what I need to modify then compile and copy the classes that I changed into the directory where the classes in use are stored.
So I created the dummy project and copied the packages/files into the source folder of the dummy project but I keep getting a "package org.jdom does not exist" error.
The error appears on the lines:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
When I copied the folders/files into the source folder I copied the entire "org" directory.
You can see from my screenshot that the package is in the Project Explorer
I have several things to resolve the issue but none seemed to work.
I tried adding the "org" directory into the classpath environment variables.
I tried adding the "org" directory as a library to the project.
I have tried clearing the NetBeans cache after doing both of these but none of these 3 things worked.
Below is a screenshot of the projects src folder in file explorer
Can someone explain to me what I need to do to get this issue resolved?
I was adding the wrong files to the libraries section of my project.
I was adding the source for jdom when i should have been adding the jdom .jar file.
Thanks for the help guys.
install jdom at your library folder, there is a new version of jdom try to download it and install it in your project directory.

My projects are not appearing in eclipse

I face strange problem, Yesterday I was checking my projects and also I created a new project to the work-set.
Today when I opened eclipse and loaded my work-set I found that my projects are not loaded into eclipse. I checked the work-set folder I found that all my files exist in the work-set folder but those projects are not visible nor loaded into eclipse as if they are not exist.
I have also more than one workset,the one that does not appear in eclipse named workset00. workset00 folder exist one the hard drive but does not appear in eclipse. Unlike others worksets such as workset01 or workset02 they
I hope the I explained my problem clearly.thanks.
check out your workspace path is set properly to the workspace ur expecting to set or it different.. please check care fully directory by directory. if everything is proper still wont work, then do the following
copy all projects some other temporary folder and delete that directory.
create new workspace in eclpise.
just copy paste all ur project to the new workspace directory.
use import project from ecplise.
and your set
Try reimporting them or import them in a new workspace.

No Package Hierarchy Importing Project in Eclipse

I've been trying to import a project that works on a friend's computer, however no matter what I try, the package hierarchy is completely lost and the result is this
Has anyone seen this issue before? I've been struggling with it for a while. Occasionally creating a new project and copying in the src folder works, but I've been unsuccesfful in my recent attempts.
The presentation of packages is a user option. Click the little down-arrow at the top of the package panel, then select "Package Presentation" from flat to hierarchical.
Turns out we weren't including the .classpath file in the source control. Facepalm. Good answers, though.
It could also be that the folders are not listed in the project as Source Folders. Go to the project properties and check that the root folders (looks like "data" and "src" here) are listed as source folders in the Build Path.

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.

Import different versions of a project into Eclipse

I have a project in Eclipse and now I want to import another project with a different name and in different location, but with a same structure(same packages, a lot of same classes...), but when I try to import it, it says " Some projects cannot be imported because they already exist in the workspace". What should I do if I want to have both of two versions of one project? What should I change? Thx.
As the projects would have the same project names in the .project file, eclipse would not allow them to be imported into the workspace.
Open the .project file for the project root, confirm and change its name and then import it.
<projectDescription>
<name>project1</name>
</projectDescription>
What you can do is create a new project with the name that you want it to be called. Once you do that right click on the new project and click import. You can then import your old project into this one which will have all the same files except a different name.
If the project has a different name that message is a bit strange. Anyway try editing the .project file in the root folder of the second project and change the value of the tag name
The .project file is hidden

Categories