Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java NetBeans Import

  1. Aug 4, 2010 #1
    I have a rather large list of classes that are have created and rather than compiling the classes that are needed with the main code I would rather use import. The problem is I do not know how to make that reference.

    My end goal is to have a package called someClass and a file named
    c:\myClasses\someClass\someClass.java
    And within my main code, reference that file by
    import someClass;

    I am new to netbeans so I am not sure if something like that needs to be set in netbeans or in some environment variable like classpath. I have already tried setting my classpath however I did not anticipate it working and it didn't.

    I am willing to try anything including dropping netbeans and using another IDE if someone knows how to do this in another program like eclipse or something.

    Thanks for any help!
     
  2. jcsd
  3. Aug 4, 2010 #2

    Filip Larsen

    User Avatar
    Gold Member

    I have some trouble understand what are asking about precisely, but it seems like you may want to read up on how Java packages works. If you don't have a good text book on Java you should be able to get the basics on the normal Java language tutorial on packages, http://download.oracle.com/javase/tutorial/java/package/index.html
     
  4. Aug 5, 2010 #3
    ok I will rephrase... I was kind of in a hurry when I posted it.

    Lets say I have a bunch of classes within a package

    package someClass;

    public class classOne() {
    }

    package someClass;
    public class classTwo() {
    }

    etc..

    Days after making this package I decide to write another program that needs to use this package. So I say

    import someClass;

    However this does not work because I have not told NetBeans where this package is located. So my question in a nutshell I guess is where do I put this package at so that netbeans will recognize it as a valid package when I try to import it.
     
  5. Aug 5, 2010 #4

    Filip Larsen

    User Avatar
    Gold Member

    It still sounds like you could benefit a lot from reading the tutorial I linked to. Given that you struggle with this issue you probably also want to follow general Java and/or Netbeans introductions or tutorials, like for instance http://java.sun.com/developer/onlineTraining/tools/netbeans_part1/.

    Regarding your direct question: if you have the Java class with the fully qualified name "my.package.MyClass" on your class path, you can import it in the Java source in another package by saying "import my.package.MyClass;". Note, that you can always refer to a class by its fully qualified name; importing is only to allow you to refer to it without a package name.

    By the way, you should probably not name a package "someClass" as a package is not a class, and it is also strongly advised not to use capital letters in package names.
     
  6. Aug 5, 2010 #5
    Thanks Filip. You are right I would never name a package "someClass" as a package is not a class as you say. I was more or less sticking with my rushed original post names where I had named the package "someClass".

    Thanks again for your help Filip.
     
  7. Aug 13, 2010 #6
    I've been playing with Netbeans a lot lately. It has some nice features for managing libraries. Look in the "Projects" window. If you don't see the "Projects" window then click on the "Window" menu at the top and select the "Projects" button to make the "Projects" window reappear. You'll see some names for the projects that you have created with NetBeans. Click the plus arrow next to the one you wish to add a library to. Depending on what type of project it is, you should see some folders like "Source packages", "Test Units", and "Libraries". Right click on the libraries folder. You should see three options.

    1) "Add Project" You can select another project to be a library for the current project. This option is good because if you update the library then dependent projects will be updated too.

    2) "Add Jar" This option will copy any jar on your computer and make it available as a library to your project. It's a one shot deal. If the original library is updated then Netbeans won't know that the copy in the current project is out of date (you'll have to delete the lib and re-add it).


    3) "Add Library" This lets you search through Netbeans' global packages for an official NetBeans library (and some other stuff) that you may want to use. It's useful for making applications that use the NetBeans tech. You can add your own libraries to the global list if you want. That way you can use the quick search that can find packages by locating a specific class name inside any globally registered packages.

    When Netbeans builds a project into a zip distribution, it will pack up all your project jars and also place all the needed libraries in a properly linked /lib folder so you have everything that you need to run the app anywhere.

    edit: You still have to use "import SomePackage.SomeClass;" to use the library in a source file. NetBeans can automatically generate imports (and remove unused imports) by pressing CTRL+SHIFT+I. Eclipse can do it too but I can't remember the shortcut.

    Also, go through NetBeans basic coding tutorials. It has plenty of time saving features that you would likely never notice on your own.
     
    Last edited: Aug 14, 2010
  8. Aug 15, 2010 #7
    Thanks zomgineer! Thats what I have been looking for! I had actually just stopped using netbeans and went back to command line but I am going to give netbeans another try now.

    Thanks again!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java NetBeans Import
  1. New to NetBeans (Replies: 2)

Loading...