| Author |
importing multiple classes from a package
|
Hari Kumarkar
Greenhorn
Joined: Aug 11, 2006
Posts: 19
|
|
Hi,
I'm using d:\corejava as a current and running folder, where i had set path as "d:\corejava>set path=c:\java\jdk1.6.0_24\bin;".
I created two helper java files i,e D:\corejava\A.java and D:\corejava\B.java and executable java file as "D:\corejava\TestPackage3.java"
I compiled A.java and B.java as "javac -d . A.java" and "javac -d . B.java" Upto here it is good.
Step 1:-
I try to set classpath as "D:\corejava>set classpath=D:\corejava;.;" then I compiled TestPackage3.java as "javac TestPackage3.java" here i got error as
TestPackage3.java:6: cannot access A
bad class file: .\A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the classpath.
A a = new A();
^
1 error
Step 2:-
Again I try to set classpath as "D:\corejava>set classpath=D:\corejava\com;.;" then I compiled TestPackage3.java as "javac TestPackage3.java" again I got the same error as above
Step 3:-
Again I changed "import com.*" to "import com.A; import com.B" then it is working fine.
Now I want to know is there is any errors in executable file or in helper file or in classpath setting. Anticipating a positive reply.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
Your folder structure needs to match the package structure. So, for instance, if A is in package com, it needs to be in a subfolder com.
So if you create D:\corejava\com, and move A.java and B.java into it, that should do the trick.
|
 |
Hari Kumarkar
Greenhorn
Joined: Aug 11, 2006
Posts: 19
|
|
Hi Matthew Brown,
Step 3 is working fine, where I explicitly imported "import com.A;" and "import com.B;". But I want it to run through "import com.*;"
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
You are usually better with import com.A; than import com.*;, but please supply more details. If import com.A; works, then import com.*; should work too. Is there something peculiar about your folder structure? Have you got some sort of spelling error?
|
 |
 |
|
|
subject: importing multiple classes from a package
|
|
|