Importing packages in Java from other folders than the working directory

Discussion in 'Mac Programming' started by Jiddick ExRex, Apr 20, 2008.

  1. Jiddick ExRex macrumors 65816

    Jiddick ExRex

    Joined:
    May 14, 2006
    Location:
    Roskilde, DK
    #1
    So I have had enough of most IDEs and would like to stay clear from them as I have discovered the power of using Emacs/Aquamacs for typing pretty much anything.
    Thus I need some sort of introduction to how to import packages (java files) from other folders than the directory I am working in.

    My current working directory:
    ~/Documents/Java/HelloWorld/

    Say I need to import ' edu.umd.cs.piccolo.nodes.PText ' from the folder ~/Documents/Java/piccolo/build/...
    (I don't want to have to copy the whole directory to the /HelloWorld/ folder although that works fine. I need to import independently of projects/folders/current applications), how do I go about doing this?

    In the file ~/Documents/Java/HelloWorld/HelloWorldExample.class I need to import it by writing import edu.umd.cs.piccolo.nodes.PText; but this clearly doesn't work, as I need to point a directory level up and then further into the piccolo/build/edu/... folders.

    What is the syntax for doing this? In the terminal it would be ' ../ 'but it obviously doesn't work, since traversing directories/packages is done with ' . ' .

    Help would be much appreciated, since I cannot seem to google answers to this...
     
  2. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    Wouldn't you just change your classpath variable?

    You can also add the classpath on the java command line

    java -classpath /Users/myusername/Documents/Java/piccolo/build/

    or in terminal

    export CLASSPATH=~/Documents/Java/piccolo/build/
     
  3. Jiddick ExRex thread starter macrumors 65816

    Jiddick ExRex

    Joined:
    May 14, 2006
    Location:
    Roskilde, DK
    #3
    Thanks.

    Even after exporting to said directory, and running ' java ~/Documents/Java/HelloWorld/HelloWorldExample.class ' I get a noClassDefFoundError. It makes it compile though.
     
  4. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #4
    I wouldn't bother with modifying the ENV variables. Instead, you will need to use the -classpath option on both javac and java commands.
     

Share This Page