• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

importing multiple classes from a package

 
Hari Kumarkar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic