Results 1 to 4 of 4
  1. #1
    circle is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default Beginner issues: Packages, classpaths and JARs

    Hi all,

    I'm currently trying to get the hang of packages, classpaths and JAR files.

    Question 1:
    I have this structure on files:

    /somepath/mytool/MyTool.java
    /somepath/mytool/gnu/getopt/Getopt.java
    /somepath/mytool/gnu/getopt/LongOpt.java
    /somepath/mytool/gnu/getopt/...

    MyTool.java has: "package mytool" and imports gnu.getopt.*

    Getopt.java and its fellow files have: package gnu.getopt

    So, what I want to do in the end is to add everything into a "mytool.jar" file.

    I try: javac -cp mytool mytool/*.java
    Issues no errors, but I feel it in my bones that this is not the way to go, which is also confirmed when trying to run it.

    I think I've got the hang of it when creating the actual JARs:
    jar cvf mytool.jar mytool/*.class
    Correct?

    Question 2:
    Say if I have a third party library like: java-getopt-1.0.9.jar
    and I want to use it in my project. I add the following import:
    import gnu.getopt.*

    But how on earth will the actual building of the project look like?

    And finally, how would one specify the class paths when running the app?

    What is the proper procedure?

    Hope you understood the core problem in my questions. Feeling a bit lost.

    Best regards
    /T

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Question 1.

    You can compile multiple files as you did, no harm there at all. That's the easiest way to do. Or else you've to specify each file with the correct path.

    When creating the JAR file you can specify the folder structure you wish to maintain.

    Creating a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)

    Check the given example in the above page.

  3. #3
    circle is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Ok, I think I've got the hang of it in the two scenarios:

    1:
    I was making it wrong the first time trying to compile it like this:
    Java Code:
    javac -cp mytool mytool/*.java
    The error I got from this was an incorrect class path error involving "mytool/MyTool".

    Instead I went into the directory "mytool" and did the following:
    Java Code:
    javac *.java
    and then still in this directory:
    Java Code:
    jar cmf mymanifest mytool.jar *.class
    Which produced a runnable jar-file trying to use it with: java -jar mytool.jar
    On the way I also learned that a manifest pointing out the main class should be included as well.

    However, I still need to test some things. Because, I should be able to be outside the directory containing the code, right? And compile things and create JARs correctly.

    2:
    Regarding the second question I also think I figured it out. Running the following:
    Java Code:
    javac -cp .;java-getopt-1.0.9.jar MyTool.java
    also did the trick I think, and the running it with the following:
    Java Code:
    java -cp .;java-getopt-1.0.9.jar MyTool
    I missed the . (dot) and the ; (semi colon) part it seems.

    Many thanks for pointing me in the right direction!
    Have a nice weekend!

    Best regards
    /Thomas

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Help understanding packages and classpaths
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 10:22 PM
  2. Java Packages/Jars
    By Silentstormz in forum Advanced Java
    Replies: 4
    Last Post: 09-14-2008, 08:58 PM
  3. Replies: 0
    Last Post: 08-19-2008, 05:44 PM
  4. Replies: 3
    Last Post: 02-12-2008, 12:03 PM
  5. What are classpaths!?
    By jimm1 in forum New To Java
    Replies: 1
    Last Post: 02-03-2008, 06:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •