How to import user-defined package in netbeans - java

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

Related

How can I get my imports in eclipse to work?

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.

How can I refer to a class from another project in the same Workspace in Eclipse/Java

I have two projects in my Eclipse ADT Android/Java workspace
MyProject
MyProjectAutomatedUITests
In MyProject, there is a package .constants with a class ButtonNames.java that is full of constants
I want to be able to use the members of that class from MyProjectAutomatedUITests
How can I do that?
You have to add your MyProject to the build path of your MyProjectAutomatedUITests by right clicking the project than Properties>Build Path>Projects>Add and select your project, by doing this you can use the classes of MyProject.
But if you just want only one class than you should create another one for your ptoject.

changing java project module hierarchy

when I create a new java project in intellij the module is: com.example.ProjectName .
how can I use my own domain?
like:
us.mysite.ProjectName
I've already tried to look for it and didn't found the answer.
This is the name of the java package. In order to change the package, the package foo.domain.project line must be changed along with the name of the directory in which the code lies. As seen here packages can be created with varying names through IntelliJ(including possibly when creating a new project), or a file can change:
package foo.domain.project;
to
package bar.dom.proj;
and move sources from the src/foo/domain/project directory to the /bar/dom/proj directory.
There is lot of useful information available on Netbeans refactoring wiki. Here is how you can rename your package
Step1 : Right-Click on the package name, in the Refactor menu select
Rename (Ctrl+R)
Step2 : In “New Name” enter the new name that you desire for the
token. Then click on Refactor button complete the operation.
Optionally you preview the effect of the refactoring.

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