File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes importing multiple classes from a package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "importing multiple classes from a package" Watch "importing multiple classes from a package" New topic
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: 4490
    
    8

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: 40041
    
  28
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: importing multiple classes from a package