Results 1 to 11 of 11
- 02-26-2013, 06:18 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
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.
- 02-26-2013, 06:27 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 658
- Rep Power
- 1
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.
must be saved in a file named MyClassName.java.Java Code:public class MyClassName { .... }
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-26-2013, 06:28 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
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.
- 02-26-2013, 06:29 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
- 02-26-2013, 06:54 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
- 02-27-2013, 09:38 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
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.
- 03-01-2013, 10:32 AM #7
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
Re: understanding java packages ...(confused with import command)
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..-:
- 03-01-2013, 11:03 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
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.
- 03-01-2013, 02:02 PM #9
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
- 03-01-2013, 02:15 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,447
- Rep Power
- 16
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.
- 03-01-2013, 02:46 PM #11
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Import packages in JSP
By drw4d in forum New To JavaReplies: 1Last Post: 09-29-2011, 10:11 AM -
confused about packages and import
By nttaylor in forum New To JavaReplies: 7Last Post: 12-14-2009, 05:55 AM -
still confused about packages and import
By nttaylor in forum New To JavaReplies: 1Last Post: 12-13-2009, 10:27 PM -
import packages
By gregorio99 in forum New To JavaReplies: 3Last Post: 11-06-2009, 12:43 PM -
Help understanding packages and classpaths
By porchrat in forum New To JavaReplies: 2Last Post: 04-24-2009, 09:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks