How can I get my imports in eclipse to work? - java

I was having trouble importing classes, I was able to import the normal java classes, but unable to import foreign classes.
Picture of imports in Eclipse

Your picture shows, that DeflateSerializer class is in wrong package.
The first line is not matching the package name, you should rename it (the default package), so that it matches.
Your picture shows that you have no dependency to the library or project, which contains the "esotericsoftware" classes. So you need to import them:
Right click with mouse on your project => "Properties"
In the following window select "Java Build Path" and then tab "Libraries".
Here you can add for example jar files to your project, which contain the imported classes.
Or you could use a build tool like Maven or gradle, but based on your question this is something for the future.

Related

How do I set up extJWNL in a Java project?

I am trying to determine whether a set of strings, from an English sentence, are all words from the WordNet dictionary. I put the JARs in a folder in my project in eclipse.
I've downloaded the binary release of extJWNL and put the JARs in a folder called lib.
I also added these jar files to the class path and the module path using Right Click > Build Path > Configure Build Path:
I used the following code to try to import dictionary
import net.sf.extjwnl.dictionary;
This error is shown on the import statement
The package net.sf.extjwnl.dictionary is accessible from more than one module:
<unnamed>, extjwnl
I thought that this error was showing up because it was a package, not a class/type. But adding a new class and trying to change the package doesn't show this new package, namely WORDNET_JARS, just the default package.
Why is this error being returned and what do I need to do to get rid of the error and import the wordnet packages?
Platforms
I am using Eclipse IDE, and write all this code in Java. The API I am trying to import is the WordNet API.
Edit (8/21/2019)
By removing the module path, it gives a new error:
Only a type can be imported. net.sf.extjwnl.dictionary resolves to a package
I'm surprised it was this easy:
Instead of
import net.sf.extjwnl.dictionary
I had to do
import net.sf.extjwnl.dictionary.*
to get all the types. The only other thing that had to be done had already been done where I add the JARs to the class path

Removing Eclipse package name

Why do I get package name in the top of the page whenever I try to create a java class?
I have written some codes in the past using Eclipse but it had no package name like this. I am just wondering why?
You can see the following.
Your class in contained in the package named asd. This is also shown on the left hand side: project "asd" / package "asd" / Class "Ex1.java"
If you want to get rid of the package, you need to move your class to "default package". Right click on the class -> Refactor -> Move -> choose (default package)
However, I would recommend to work with packages and name them properly. Putting classes into "default package" is considered bad practice.
As you can see in package explorer
you placed your class inside asd package. This happened when you created your class via File->New->Class wizard. If you don't specify any package then by default it will use project name.
So when you confirm such configuration such setup Ex1.java will be placed inside asd folder (package), which means that your code will need at its start information about name of package.
If you don't want to have this package asd; line you can for instance
clean that section from wizard when you create package,
or even remove that line from your code and when Eclipse will ask you how to fix this situation
place cursor in line with problem (first line)
select Edit->Quick fix, or press Ctrl+1)
and chose Move Ex1.java to default package.
But you should avoid using default package. For instance when you export classes from your project to Jar file you will not be able to import them in new projects.
For more info about packages visit official tutorial:
What Is a Package?
Lesson: Packages
All Java classes have specified their address in the beginning of the document. They say
package package_name;
If the class is in the default package (directly in 'src' folder), then there is no package written in the beginning of the class...
You can see here, and try on your own:

How to import user-defined package in netbeans

I have a application whose name is javaapplication3. I want to create an object of javaapplication3 in another application whose name is javaapplication6. How do I add this user-defined package in javaapplication6?
If those packages are in the same project:
import javaapplication3.*;
If the packages are in different projects, you have to build a Jar from application 3, then add this jar as a dependency to the project application 6.
A third (and dirty) solution would be to copy paste package javaapplication3 to you new javaapplication6.
Right click on "Source PAckages" -> "New" -> "Java Package..."
enter any name you like?! :-)
Edit after first comment:
You have to go to project properties -> Libraries -> Add Project
and add the other project.
Although this question has been asked a long time ago. There was no clear answer that could solve the problem. So:
To import your own jar or Java class you use Project properties in the file menustrip on netbeans. You create your library, say MyLibrary, and add classpath and jar file sources

The import javazoom cannot be resolved

hi all
i use the javazoom.jl.player.Player package but it is says The import javazoom cannot be resolved. i am using eclipse and doing Android project. i clean the project still the same error is shown. please correct me.
If eclipse can't resolve a package fragment of an import statement, then it tells you (with that error), that there is no library on the classpath that contains a class from that package (or from a package whose name starts with the missing part).
An easy way for standard java/eclipse:
create a folder lib in your projects root directory (with the eclipse workbench!)
copy and paste the jar into that folder
right-click the copied jar and select "add to build path".
This should eliminate the compiler errors immediately.
(Previous part of the answer)
Taking the error message literally, it looks like you have a line of code like that:
import javazoom;
This would be wrong, because we don't import packages but classes from a package. To import all classes from the javazoom package, we'd say:
import javazoom.*;
You should download the .jar of jLayer ( http://www.javazoom.net/javalayer/sources.html )
And add into classpath in the way Andreas_D told you.

HTTPCLIENT does not exist? Netbeans

I am trying to import:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
But I am being told these do not exist?
I downloaded:
httpclient-4.0.1.jar and httpmime-4.0.1.jar
... and placed these in the same folder as my .java files that are trying to use httpclient.
Any ideas?
I still cannot get it to work... Within the folder "Libraries" I have: apache-mime4j0.6.jar commons-codec-1.3.jar commons-logging-1.1.1.jar httpclient-4.0.1.jar httpcore-4.0.1.jar httpmime-4.0.1.jar For the java file properties it has: compile classpath runtime classpath boot classpath In each of those, it seems to refer to the jars I have imported. Still getting does not exist. :-(
I have tried to do this in Eclipse too and now those files appear in "Referenced libraries" however it still doesn't work. lol
The two jars you have mentioned need to be placed in the classpath of the project in Netbeans, not in the source directory.
In my Netbeans 6.7.1 on Mac, in the Prjects tab, you cna right click on the project and select Properties. That will bring up the project properties dialog. In there, choose the libraries item from the tree on the left. From there, choose the Add Jar/Folder in the Compile view. To add the jar to your project, use the chooser to locate it and then select it.
EDIT:
I have just downloaded the HTTPClient package and I think I see the problem:
in 4.0.1, the package structure is not as you have it defined. Instead of:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
use:
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.params.HttpMethodParams;
In Eclipse, press Ctrl + Shift + O to organize your imports. This will look for all unknown classes on the classpath and try to import them. You can also place your cursor on a class name and press Ctrl + Shift + M to attempt to import that single class. This is sometimes helpful for class name collision (i.e. if two packages have a HttpClient class, you can click on the desired class).
If the jars are in Referenced Libraries, then they should be on your classpath. You can verify this by right clicking the project and selecting something like Build Path > Configure Build Path, then click the libraries tab.
Also, you probably have build automatically selected by default, but if you don't, you'll need to build your project. You may also want to attempt to clear the build path and re-build it. I've seen my Eclipse get out of synch a few times and this fixed it, albeit somewhat of a fluke.
If you're using Maven, this sort of thing can sometimes occur if you have an incorrect dependency scope (i.e. runtime, or test vs. compile).
For what it's worth, unless you're utilizing the entire package, there is no reason to import an entire package's contents (i.e. import package.*).
It seems that HttpClient has changed his sintaxis from 3 to 4 version... I had same problems that all of you trying to import packages until I found this example:
http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/
This is sample is Android oriented but works on any Java Application!!! Im using netbeans 6.9.1, httpclient-4.1.1.jar, commons-codec-1.4.jar and commons-logging-1.1.1.jar
Hope you can solve your problems!!!
Had the same problem and i managed to get the solution. Here it is:
1) Download the org.apache.commons.httpclient.jar.zip file from
http://www.java2s.com/Code/Jar/o/Downloadorgapachecommonshttpclientjar.htm
and save it anywhere on your computer.
2) Right click on your NetBeans project and select Properties
3) On Project Properties Categories, select Libraries
4) Click the Add JAR/Folder button
5) Now browse to the file location where you saved your downloaded org.apache....jar.zip
file and click open button.
6) Now the file has been installed, click OK and you are done.
Note that this might require you to restart your netbeans IDE.

Categories