Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By JosAH

Thread: Packages and imports question

  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Packages and imports question

    Hello, I have problem with packages and imports. It`s trange I can use a lot of intermediate techniques but packages and imports are still a complete mess for me. Consider this case: I have linux distro OS, on my Deskotp I have an empty folder let`s say Project. So I`ve started to code plain text files ( java code obviously ) and let`s say they are over 10. So... I`ve tried to make them all "package com.ilian.Project" but it appears to be an error. Tried the full path name by dir like home.ilian.Destkop.Project but still nothing. Just errors for missing class names or sth like that. So tell me - in that case: an empty folder with files from scratch - how to organize that mess in packages ?
    Will be most grateful to receive a good example and explanation. Please don`t point me Oracle`s tuts, I`ve tried them - not working.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Packages and imports question

    Package names should be reflected in the directories where the files are stored; i.e. a fully qualified class name com.acme.MyClass should be stored in a directory <X>/com/acme/MyClass.java. You should set the classpath variable to make it point to <X> and you can compile the file as "javac -cp <X> com.acme.MyClass.java" and run it as "java -cp <X> com.acme.MyClass" because the compiler stores the compiled version of your source next to the source file (by default). You should always import the classes that are stored in a non-default package, i.e. not stored in directory <X> itself. Also see "javac -help" and "java -help".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    Thanks for the reply, but what happens when I give you the program for example, and you have a different path name? Do I have to make a jar file and some manifest adjustments?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Packages and imports question

    Quote Originally Posted by heatblazer View Post
    Thanks for the reply, but what happens when I give you the program for example, and you have a different path name? Do I have to make a jar file and some manifest adjustments?
    If you give me the source file, I have to store it in <X>/com/acme/MyClass.java and compile it etc. If you give me just the compiled class MyClass.class, I have to do the same before I can run it; if you give me a .jar file with a Manifest in it (pointing to the class that contains the main( ... ) method), I can store it anywhere and run it. ("java -jar YourJar.jar").

    kind regards,

    Jos
    heatblazer likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    Very strange issue... I`ve checked one of my Android apps, so I borrowed the idea, made sub folders src and bin and placed my package like com.ilian.Pay in both folders as subfolders respectively src/com/ilian/Pay and bin/com/ilian/Pay then placed java files in src... and compiled with plain javac *.java and everyting worked fine... I`ve tried javac -classpath ilian....bin...Pay but no output of class files were made... Strange, but kind of works... still leaves fuzz in my head, tho...

  6. #6
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    I kind of got it. I`ve used javac -d /home/ilian/Desktop/Pay/bin and it generated com/ilian/Pay/ diretcory with all classes. I think I`ll Perl-script these awkward tasks! But thanks for clarifying some mysteries I`ve fought with :)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Packages and imports question

    That's why things like ant exist.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    I`ve made a small useful tool in Perl for creating directories with sub dirs for both separating src and bin. I`ve uploaded the txt file ( no .pl extension allowed). Just rename it to .pl and on lunux chmod it ( chown it ). Haven`t tested in on Winodws tho, but it`s useful to me. Hope that it will help other ppl too.
    Attached Files Attached Files

  9. #9
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    I still can`t get how to work the JAR file tho :( What is the Class-Path: in manifest set to and what is Main-Class refer to???

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Packages and imports question

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    OK, I`ll describe it step by step so you tell me where I get wrong:
    1. I have a folder /home/ilian/PayProgram
    2. there are 2 subfolders src and bin ( in src there are .java and in bin are .class )
    3. When I started I do:
    4. javac -cp /home/ilian/PayProgram/bin *.java -d /home/ilian/PayProgram/bin
    5. All my java files are in package com.ilian;
    6. I go to src and do: javac -cp /home/ilian/PayProgram/bin com.ilian.MainFrame
    7. It starts the program
    8. I`ve created a Manifest file:
    Main-Class: MainFrame
    Class-Path: MFrame.jar
    9. jar cfm MFrame.jar Manifest.txt *.class

    And here goes nothing - there is no way I start JAR file. I`ve tried to specify main class as com.ilian.MainFrame but java -jar MFrame.jar fails... :( Really is it that awkward???

  12. #12
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    OK, I`ll describe it step by step so you tell me where I get wrong:
    1. I have a folder /home/ilian/PayProgram
    2. there are 2 subfolders src and bin ( in src there are .java and in bin are .class )
    3. When I started I do:
    4. javac -cp /home/ilian/PayProgram/bin *.java -d /home/ilian/PayProgram/bin
    5. All my java files are in package com.ilian;
    6. I go to src and do: javac -cp /home/ilian/PayProgram/bin com.ilian.MainFrame
    7. It starts the program
    8. I`ve created a Manifest file:
    Main-Class: MainFrame
    Class-Path: MFrame.jar
    9. jar cfm MFrame.jar Manifest.txt *.class

    And here goes nothing - there is no way I start JAR file. I`ve tried to specify main class as com.ilian.MainFrame but java -jar MFrame.jar fails... :( Really is it that awkward???

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Packages and imports question

    Quote Originally Posted by heatblazer View Post
    8. I`ve created a Manifest file:
    Main-Class: MainFrame
    Class-Path: MFrame.jar
    Wrong, the full name of the class is com.illan.MainFrame.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    :( Tried what you proposed, still java.lang.NoClassDefFoundError: MainFrame (wrong name: com/ilian/MainFrame)

  15. #15
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    Just got it! Very frustrating stuff in Java really! So... considering what I`ve written above. The trick is that jar must contain directory hierarchy. The way doing this stuff is
    1. cd /bin
    2. jar cfm MFrame.jar Manifest.txt *

    Yes the glob! Adding everything in JAR! Really... frustrating issue!

  16. #16
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    Just got it! Very frustrating stuff in Java really! So... considering what I`ve written above. The trick is that jar must contain directory hierarchy. The way doing this stuff is
    1. cd /bin
    2. jar cfm MFrame.jar Manifest.txt *

    Yes the glob! Adding everything in JAR! Really... frustrating issue!

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Packages and imports question

    You don't need a class path entry if the only thing is the current jar file.
    It should list dependencies only, and you don't seem to have any so it can be safely left out.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Packages and imports question

    Nope, I`ll need more jars inside so I`d better prepare it before I forgot how I did it :)

Similar Threads

  1. Question about Packages
    By Huskies in forum New To Java
    Replies: 2
    Last Post: 01-19-2012, 06:08 PM
  2. Beginner Question About Packages, etc
    By Humphrey Bogart in forum New To Java
    Replies: 15
    Last Post: 03-24-2011, 04:45 PM
  3. Question about packages.
    By dragonchr15 in forum New To Java
    Replies: 7
    Last Post: 08-07-2009, 11:34 PM
  4. Imports
    By whosadork in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 01:32 AM
  5. Replies: 3
    Last Post: 02-12-2008, 11:03 AM

Tags for this Thread

Posting Permissions

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