New To Java
how do I import packages
Hi, I'm new to java and I am trying to incorporate some opensource code into my application. How do I do this. Which files (.class? .jar?) of the outside code do I use? where to I put them? How do I import them? I am using netbeans4.1 on windows. The code I want to use is here http://openmap.bbn.com/ http://openmap.bbn.com/doc/api/com/bbn/openmap/util/quadtree/QuadTree.html thanks!
Look at the API documentation for the classes you are trying to import. Each class java doc should show the class package. Then, you use the fully qualified class name to import the class. For example, to use the TreeSet class, you look at the TreeSet javadoc page. It says that the package is java.util. So, your import statement would be: import java.util.TreeSet; To import all classes from a package, you can use a wild card: import java.util.*; will get all classes in the java.util. I personally don't recommend this, for reasons I will not go into just now. Note that this is not recursive. The above wildcard import will NOT get classes from package java.util.concurrent. You never import jar files. Jars containing classes must be in your classpath to use the classes, but the classes themselves must be imported into your source. BTW, you question is too general. No one knows what classes you should import. It depends on what classes you need for a specific application.
I have been unsuccessfully to do something similar. I have a jar file that has classes that I need to use, sharkura you said that the classes must be imported into the source ?, how do you mean ? In my case, I'm using netbeans and have added the jar to the projects properties, if I put the following line in my code (the name of the jar is JDatePicker): JDatePicker jdp = new JDatePicker(); it gaives the following error cannot find symbol : class JDatePicker Any suggestions ?
In addition to specifying an import statement, you have to add the JAR file or directory containing class files to the classpath. Read this: How Classes are Found
I've read the link but sorry but I'm still not sure. 'Adding the jar to the classpath' - are you saying that I must put the jar in the same directory as my other class files ? And do you need to import a jar file the same way as a package ? Clearly I need to understand classpaths better, do you know any good links where I can check it out ?
A jar file contains classes, just like a directory contains classes. The classpath tells Java where to find classes. You set the classpath to point to all the locations that contain classes that your application needs (except for the standard Java classes). In case of a jar file, you have to add the full path of the jar file, including <filename>.jar. In case of a directory, you only have to add the directory containing the top of your package hierarchy (e.g. if your code is in D:\projects\myproject\com\example\ and you packages start with com.example, you have to add D:\projects\myproject to the classpath). This should help you get started: http://java.sun.com/docs/books/tutorial/
Sorry, I'm also still having trouble. I want to include the following class: com.bbn.openmap.util.quadtree. Does this mean I put: import com.bbn.openmap.util.quadtree; in my own program? Where exactly should I put the jar file containing this class? And how do I get my program to find it? I'm using netbeans and I added the jar in the Java Platform Manager and Library Manager, but I still get the error saying "package com.bbn..... does not exist". What am I doing wrong?
This statementimport com.bbn.openmap.util.quadtree; attempts to import a class called quadtree from the package com.bbn.openmap.util, whereas you want to use the class QuadTree from the package com.bbn.openmap.util.quadtree. Do either:// import all classes from this package import com.bbn.openmap.util.quadtree.*; orimport com.bbn.openmap.util.quadtree.QuadTree;
i got a package com.tempo.rempo.dempo.*; exactly where should i copy its (jar file/.rar file) to make netbeans import classes from dempo...? if i got a folder called "com" in which i got all class files..where to copy this folder to make netbeans import that classes???? if i have a com.zip file in which i have classes...can i unzip src.zip and merge this two zip files and make a new src.zip file?..will this allow me to import packages? what is the significance of creating environment variable in this problem? please write with examples....