Results 1 to 11 of 11
  1. #1
    avenge is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Unhappy understanding java packages ...(confused with import command)

    Hello guyz,am learning java,My question is simple, When i give import packagename.classname; ,the program works, but it wont when i give import packagename.*; (i..e its not working when i specify the import command for all the class files from a folder(package.*) ,but when i specify the exact path of a classfile with its name it works ...am confused,it should work in both the cases right??,i have already set the path in enviroment variable to bin directory.

    ok here is my simple code:-

    package vars;
    public class vars1
    {
    public int a=1;
    public int b=2;
    public int c=3;
    }
    //This file is saved as vars1.java in my working directory(c:/j/pp), then i compile it as javac -d . vars1.java
    --------------------------------------------------------------------------------------------------------------------------------
    package vars;
    public class vars2
    {
    public int d=4;
    public int e=5;
    public int f=6;
    }
    //This file is saved as vars2.java in my working directory(c:/j/pp), then i compile it as javac -d . vars1.java

    ----------------------------------------------------------------------------------------------------------------------
    Ok to make things clear.if you could understand.i have vars1.class and vars2.class in vars directory (c:/j/pp/vars)
    ---------------------------------------------------------------------------------------------------------------------

    Now here is my main method file which imports the above two files:-

    import vars.vars1;
    import vars.vars2;

    class mainm{
    public static void main(String argv[])
    {
    vars1 obj1=new vars1();
    vars2 obj2=new vars2();
    System.out.println(obj1.a+" "+obj1.b+" "+obj1.c);
    System.out.println(obj2.d+" "+obj2.e+" "+obj2.f);
    }
    }

    //The above file is saved as mainm.java in (c:/j/pp)

    ----------------------------------------------------------------------------------
    after compiling and executing i successfully get the output as: 1 2 3
    4 5 6
    ----------------------------------------------------------------------------------

    BUT MY PROBLEM IS WHEN I WRITE THE ABOVE CODE AS:-

    import vars.*;

    class mainm{
    public static void main(String argv[])
    {
    vars1 obj1=new vars1();
    vars2 obj2=new vars2();
    System.out.println(obj1.a+" "+obj1.b+" "+obj1.c);
    System.out.println(obj2.d+" "+obj2.e+" "+obj2.f);
    }
    }
    //The above file is saved as mainm.java in (c:/j/pp)
    ------------------------------------------------------------------------------------------
    IT WONT EXECUTE, HERE IS MY ERROR:-

    C:\j\pp>javac mainm.java
    mainm.java:6: cannot access vars1
    bad class file: .\vars1.java
    file does not contain class vars1
    Please remove or make sure it appears in the correct subdirectory of the classpath.
    vars1 obj1=new vars1();
    ^
    1 error

    ------------------------------------------------------------------------------------------
    Last edited by avenge; 02-26-2013 at 06:49 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: understanding java packages ...(confused with import command)

    You saved your second class file (vars2) in a vars1.java file. Top level public classes must be saved in a file of the same name.

    e.g.

    Java Code:
    public class MyClassName {
    
    ....
    
    }
    must be saved in a file named MyClassName.java.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: understanding java packages ...(confused with import command)

    You should keep your java files in the same structure as your class files.
    The existence of the java file(s) in the same (incorrect) directory as the main.java file is confusing the compiler.
    Please do not ask for code as refusal often offends.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: understanding java packages ...(confused with import command)

    Quote Originally Posted by jim829 View Post
    You saved your second class file (vars2) in a vars1.java file. Top level public classes must be saved in a file of the same name.
    Since those two files compiled I'm guessing that's a typo in the post.
    Please do not ask for code as refusal often offends.

  5. #5
    avenge is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: understanding java packages ...(confused with import command)

    Quote Originally Posted by jim829 View Post
    You saved your second class file (vars2) in a vars1.java file. Top level public classes must be saved in a file of the same name.

    e.g.

    Java Code:
    public class MyClassName {
    
    ....
    
    }
    must be saved in a file named MyClassName.java.

    Jim
    sorry man..that is a posting mistake,i re-edited it...i did save as vars2.java and i got vars2.class file after compilation .....sorry it was my posting mistake....the error is same.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: understanding java packages ...(confused with import command)

    See my post #3.
    That's your problem.
    Please do not ask for code as refusal often offends.

  7. #7
    avenge is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: understanding java packages ...(confused with import command)

    Quote Originally Posted by Tolls View Post
    See my post #3.
    That's your problem.

    hey am still confused, the program executes when i give import packagename.classname i...e import vars.vars1; (importing only single class file from package), but not when i write import.*; ....i.e importing all classes from a package ....if the program executed well when importing a single class from a package then it should also execute when importing all the class files from same package .....right??????....that is my doubt...pls help me to clear it...its driving me crazy..-:

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: understanding java packages ...(confused with import command)

    There are two vars1 related files in your system.
    One is the class file, which is in its correct directory matching the package structure.
    The other is the source file (vars1.java), which is in the current directory.
    With a specific class import the compiler knows to go direct to the class file when it encounters a reference to vars1 in your code.
    With the '*' import it does not know that vars1 is in that package, so the compiler starts lookign for a suitable candidate. The first one it finds is the source file, which has an invalid package declaration since it is in the wrong directory.
    Please do not ask for code as refusal often offends.

  9. #9
    avenge is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: understanding java packages ...(confused with import command)

    Quote Originally Posted by Tolls View Post
    There are two vars1 related files in your system.
    One is the class file, which is in its correct directory matching the package structure.
    The other is the source file (vars1.java), which is in the current directory.
    With a specific class import the compiler knows to go direct to the class file when it encounters a reference to vars1 in your code.
    With the '*' import it does not know that vars1 is in that package, so the compiler starts lookign for a suitable candidate. The first one it finds is the source file, which has an invalid package declaration since it is in the wrong directory.

    ok. so, what u finally mean is that i should keep both the .java(source) and .class of a program in same directory to work with '*' import....

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: understanding java packages ...(confused with import command)

    Yes.

    Which is exactly what I said in post #3 three days ago.
    Please do not ask for code as refusal often offends.

  11. #11
    avenge is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: understanding java packages ...(confused with import command)

    Quote Originally Posted by Tolls View Post
    Yes.

    Which is exactly what I said in post #3 three days ago.


    Thanks ..now my doubt is clear.

Similar Threads

  1. Import packages in JSP
    By drw4d in forum New To Java
    Replies: 1
    Last Post: 09-29-2011, 10:11 AM
  2. confused about packages and import
    By nttaylor in forum New To Java
    Replies: 7
    Last Post: 12-14-2009, 05:55 AM
  3. still confused about packages and import
    By nttaylor in forum New To Java
    Replies: 1
    Last Post: 12-13-2009, 10:27 PM
  4. import packages
    By gregorio99 in forum New To Java
    Replies: 3
    Last Post: 11-06-2009, 12:43 PM
  5. Help understanding packages and classpaths
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 09:22 PM

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
  •