Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - Importing packages?

  • 02-01-2004 2:01pm
    #1
    Registered Users, Registered Users 2 Posts: 841 ✭✭✭


    Hi all,

    HAPPY NEW YEAR!

    Anyone know how to make packages and add them to the libraries in Java (so I can import them into my classes from any folder)? I have loads of my own classes and methods that I use all the time but the only way I can access them is to put them in the same folder as the class I am currently working with. So, I end up with different versions of them all over the place!

    I am using TextPad as a simple java editor and I don't have JBuilder or anything like that!

    Any help would be greatly appreciated and very useful, thanks!


Comments

  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    add the directorys where they are stored to the classpath??

    You can just jar the packages up rather than having loads of .java/.class files hanging around


  • Registered Users, Registered Users 2 Posts: 841 ✭✭✭Dr Pepper


    Thanks,

    But whats this 'classpath' business? How do I add the directories to it.

    I'd rather not jar or zip the classes so I can edit them regularly without a big rig-ma-role!


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Just off the top of my head.... :)

    If you're using a text editor and the command line to compile, you're better off with batch files that will do all the compiling for you, and in the right order, that is, if class X uses an instance of class Y, class Y must be compiled before class X.

    Typing 'java -help' or 'javac -help' will give you a nice big list of your options.
    If you're using an IDE such as JBuilder, all of the guff with packages should be done for you, but if you have to present or distribute your work, then you will have to take all of your source files when tested, and recompile and jar them with the command line (JBuilder, for me anyway, tends to do some funny things when compiling).

    With classpaths, IIRC, if you have a package called com.my.package, then when you compile the sources of classes declared to be in this package, the class files will automatically be put into a subdirectory called .../com/my/package/. Where this subdirectory is placed, you can specify at compile time, but in order to use the packages, you must tell both the compiler and the VM where they can find these classes, ie their classpath. The classpath is the location of the package subdirectory...e.g., as above if, the classes are compiled to c:\javaproject\com\my\package\, then your classpath is c:\javaproject\.

    (Someone correct me if I'm wrong here)


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Also get eclipse. It will help you out.


  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    Originally posted by seamus
    If you're using a text editor and the command line to compile, you're better off with batch files that will do all the compiling for you, and in the right order, that is, if class X uses an instance of class Y, class Y must be compiled before class X.
    This isn't necessary, javac is quite smart and does this for you. For a serious Java build system look at Ant

    You are correct about the classpaths, except that if you are hacking using a text editor you will need to create the directory structure for packages yourself. Also you will need to put the source files in the directories that correspond to their packages.

    i.e. com.blah.package.class would be in com/blah/package/class.java


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 841 ✭✭✭Dr Pepper


    Thanks a million guys. I'm gonna go make myself a big mug o' coffee and get stuck into this again. I'm in one of those 'Not-leaving-until-I-get-this-stupid-thing-working' kind of moods.

    I should really be using better software for a developement environment but I'm scared of them :( I downloaded JBuilder recently but it was so full of options, I got p***ed off and lost the patience for it! Couldn't even compile a simple class. It seems like the kind of thing you need to learn in college or somethin!

    I'll have a look at Ant and see if I can get my head around that.


  • Registered Users, Registered Users 2 Posts: 6,334 ✭✭✭OfflerCrocGod


    I endorse Eclipse.org (maybe a bit too much for you but check out the tutorial that comes with it).


  • Closed Accounts Posts: 1,135 ✭✭✭KlodaX


    if you are using textpad you must be using the dos prompt to compile?
    javac programName.java
    java programName

    in order to get the j2sdk working you would have set path=.. etc...
    to set the classpath: set classpath=c:\java;.;

    in c:\java\ you put your packages... the folders that contain your classes ...

    ie. myPackages is the name of the folder, first is the name of the package

    c:\java\myPackages\first

    at the top of your program using you package use:

    import myPackages.first;


  • Registered Users, Registered Users 2 Posts: 491 ✭✭Silent Bob


    Originally posted by Dr Pepper
    I should really be using better software for a developement environment but I'm scared of them :(
    My summer job entailed using vim, java, javac and Ant :)

    Are there better environments out there? Probably (I'm very productive with vim though:)). Would anything else run on a P100? I seriously doubt it...


  • Registered Users, Registered Users 2 Posts: 841 ✭✭✭Dr Pepper


    Finally sorted :D

    Thank you all very much for the help. Learned a lot!

    For those of you who care: Textpad has 'Compile Java' and 'Run Java App.' options which are very handy. I got my classes to import from mypackages in CMD (thanks KlodaX) but couldn't find the packages when compiling in textpad (It didn't seem to be using the classpath I declared in CMD). Eventually, I messed around with the options and set '-classpath %classpath%;.;c:\java $File' as the parameter for compiling and '-classpath %classpath%;.;c:\Java $BaseName' as the parameter for running.. Simple as that :)

    Still plan to check out Ant, Eclipse, and Vim though, cheers


  • Advertisement
Advertisement