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 7 of 7

Thread: How do I import user defined packages to my java application.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I import user defined packages to my java application.

    I have created a package in c:\world and I want to import it to my java source file stored in d:\java. It says that unable to access the package c:\world\Balance.class. What do i need to do??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I import user defined packages to my java application.

    Is the class in a package? Is the package path on the classpath when the program is compiled?

    Please post the text of the import statement and show the command line when the javac command is issused and show the folders and paths when javac is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I import user defined packages to my java application.

    Quote Originally Posted by Norm View Post
    Is the class in a package? Is the package path on the classpath when the program is compiled?

    Please post the text of the import statement and show the command line when the javac command is issused and show the folders and paths when javac is executed.

    Yes the class is in the package world. And I have set the classpath as C:\world.
    In the first attempt i used the import statement as "import world.Balance"
    In the second attempt i used "import Balance"
    But none of them worked. I got the same error in both of the attempts.
    The error was that "world.Balance.class cannot be accessed,bad class name"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I import user defined packages to my java application.

    the class is in the package world. And I have set the classpath as C:\world.
    The classpath should point to the folder containing the package path: C:
    the world folder should not be part of the classpath.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I import user defined packages to my java application.

    Quote Originally Posted by Norm View Post
    The classpath should point to the folder containing the package path: C:
    the world folder should not be part of the classpath.
    I have tried this also but this didn't work!!

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How do I import user defined packages to my java application.

    Here is what I did on my Windows XP workstation:

    I have a folder F:\home\Zaphod

    In F:\home\Zaphod I created a file named Example.java

    Here's what is in that file:
    package world;
     
    public class Example
    {
        public static void main(String [] args)
        {
            System.out.println("This is an example from Zaphod_b.");
        }
    }


    Here is the session where I compiled and executed: Computer output is blue, my input is black.


    F:\home\Zaphod>dir C:\world
    Volume in drive C has no label.
    Volume Serial Number is 843A-0C08

    Directory of C:\world

    10/11/2012 10:42 AM <DIR> .
    10/11/2012 10:42 AM <DIR> ..
    0 File(s) 0 bytes
    2 Dir(s) 6,137,946,112 bytes free

    F:\home\Zaphod>javac -d C:\ Example.java

    F:\home\Zaphod>dir C:\world
    Volume in drive C has no label.
    Volume Serial Number is 843A-0C08

    Directory of C:\world

    10/11/2012 10:43 AM <DIR> .
    10/11/2012 10:43 AM <DIR> ..
    10/11/2012 10:43 AM 447 Example.class
    1 File(s) 447 bytes
    2 Dir(s) 6,137,946,112 bytes free

    F:\home\Zaphod>java -cp C:\ world.Example
    This is an example from Zaphod_b.



    Note that if I didn't already have a directory named C:\world, it would create an empty directory at the time I executed the javac command as shown above on a source file that has a package world; line.

    Note, also, that Windows ignores case in file names and directory names, but Java does not (that's not) ignore case in class names and package names.

    So, for this class name, Example, the following does not work:

    java -cp C:\ world.example

    Java gives a very informative Error message that starts:

    Exception in thread "main" java.lang.NoClassDefFoundError: world/example (wrong name: world/Example)

    Also, since the package name is world, it won't work if you try:

    java -cp C:\ World.Example

    Etc.

    My setup:
    Windows XP Service pack 3
    javac.exe and java.exe version 1.6.0_33

    YMMV (Your Mileage May Vary)

    Now, stop here if any of this stuff doesn't work or doesn't make sense on your system. It's got to be right before we get to the Good Stuff.

    If you want to call a function in the world package rather than just executing a main() function from a particular class in the package, see my next post.


    Cheers!

    Z
    Last edited by Zaphod_b; October 11th, 2012 at 05:07 PM.

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How do I import user defined packages to my java application.

    If everything in my previous post worked for you, then try the following to be able to invoke a method in a package class.


    Change Example.java to the following:
    package world;
     
    public class Example
    {
        public static void main(String [] args)
        {
            System.out.println("worldExample.main(): This is an example from Zaphod_b.");
        }
        public static void printMessage()
        {
            System.out.println("world.Example.printMessage(): This is an example from Zaphod_b.");
        }
    }
    Compile with javac as before.

    Create a new Java source file:
    package world;
     
    import world.*;
     
    public class UseExample {
        public static void main(String [] args) {
            Example.printMessage();
        }
    }

    Compile with javac in a similar manner.

    Here's my session with the new Example.java and UseExample.java:

    F:\home\Zaphod>javac -d C:\ -cp C:\ UseExample.java

    F:\home\Zaphod>javac -d C:\ -cp C:\ Example.java

    F:\home\Zaphod>java -cp C:\ world.UseExample
    world.Example.printMessage(): This is an example from Zaphod_b.

    F:\home\Zaphod>java -cp C:\ world.Example
    world.Example.main(): This is an example from Zaphod_b.


    If you want UseExample to be in a different package, then you can try something like

    package anotherworld;
     
    import world.*;
     
    public class UseExample {
        public static void main(String [] args) {
            Example.printMessage();
        }
    }

    Use exactly the same javac command line, but now it will create UseExample.class in C:\anotherworld, and you can execute it with the following command:


    java -cp C:\ anotherworld.UseExample



    Cheers!

    Z
    Last edited by Zaphod_b; October 11th, 2012 at 02:21 PM.

Similar Threads

  1. Need help with passing user defined variable in SQLJ
    By redkryptonite in forum JDBC & Databases
    Replies: 1
    Last Post: March 7th, 2012, 05:47 AM
  2. [SOLVED] Printing an ArrayList of user-defined Objects
    By er1111 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 2nd, 2012, 11:06 AM
  3. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 10:23 PM
  4. [SOLVED] Confuced when import packages...
    By Delmi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 20th, 2010, 03:39 AM
  5. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM

Tags for this Thread