Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to have 2 packages in project

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default how to have 2 packages in project

    hi i have writen 2 programes and i want to kick on off when the other finishes but cant seem to find a way to get it working basically i have
    package a;

    imports

    class{main}
    class{}
    class{}
    package b

    imports

    class{main}
    class{}
    class{}

    the problem is package b cant be added to white space after first package so how would one run a package from another package
    the error it gives is :
    class, interface, or enum expected


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: how to have 2 packages in project

    Quote Originally Posted by bean View Post
    the problem is package b cant be added to white space after first package so how would one run a package from another package
    What do you mean added to white space?
    Quote Originally Posted by bean View Post
    the error it gives is :
    class, interface, or enum expected
    Are you trying to include everything in one file?

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: how to have 2 packages in project

    Quote Originally Posted by jps View Post
    What do you mean added to white space?
    Are you trying to include everything in one file?
    sorry white space as in at the end of the last class in first package and yup trying to have all in one file.
    obiuosly a soultion is combine the to packages and have main in package b just a method to run that packages classes however unfortunitly an error in first package that i ould never solve required me to system exit(0) out of first package so i want to kick off second package just before system.exit(0) which is a bit messy but the only option i can think to solve my issue as if i kick it off in same package then system exit will kill it

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: how to have 2 packages in project

    When you declare a package, you are saying "This file will be inside this folder" where the "file" is the body of text you are typing on and "folder" is the folder the file is saved in. You can not tell the compiler to look for the file in two folders. This is why it shows as an error. How would the compiler choose which folder to actually look in?

    By convention all Java classes should be defined in their own file. Do some research on Java classes and packages and organize your project to follow convention

    --- Update ---

    As far as System.exit(0) and starting a second package, I suggest you also read the API on System.exit to see what happens.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    63
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: how to have 2 packages in project

    the solution was Java shutdown hooks let that package exit(0) and run the second after compiling it from terminal with Java shutdown hooks there very usefull

Similar Threads

  1. External Packages
    By dougie1809 in forum Java SE APIs
    Replies: 2
    Last Post: March 15th, 2013, 01:51 PM
  2. Trying to Figure out Packages
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 18th, 2013, 12:33 PM
  3. packages
    By Spidey1980 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 6th, 2011, 06:45 PM
  4. Problem with Packages
    By pirezas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 12th, 2010, 02:41 PM
  5. importing packages..
    By chronoz13 in forum Java IDEs
    Replies: 4
    Last Post: November 23rd, 2009, 04:49 PM