I am trying to import all the classes present in a user defined package, by
using the statement as

import mypack.*;

but it's not working in my program.

Its Giving error :
C:\Users\Nitin\java>javac AOTest.java
AOTest.java:6: cannot access AOperation
bad class file: .\AOperation.java
file does not contain class AOperation
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
AOperation a=new AOperation();
^
1 error

Recommended Answers

All 23 Replies

Where is the class: AOperation defined?
Is its location on the classpath for the javac command?

I guess Ur class "AOperation" is sitting under sub-directory of "mypack"

I hav 2 class files in p1 package ie in folder p1.

outside folder i hav one java program importing all classes of p1 package :
i am using : import p1.*;


its not working !! so whats solution ?

Check whether your classes inside P1 are public .. Coz if it is a default specifiers then you cannot access from outside pkg

And are u using any editor ? If not then compile java files related to package p1
using -d option
javac -d . (Alljava files related to P1)
and then run like this
javac -cp . AOTest.java

Let me know result...

Its giving Error !!!

C:\Users\nHulk\java>javac -cp . AOTest.java
AOTest.java:6: cannot access AOperation
bad class file: .\AOperation.java
file does not contain class AOperation
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
   AOperation a=new AOperation();
   ^
1 error

C:\Users\nHulk\java>javac -d . AOTest.java
AOTest.java:6: cannot access AOperation
bad class file: .\AOperation.java
file does not contain class AOperation
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
   AOperation a=new AOperation();
   ^
1 error

I guess Ur class "AOperation" is sitting under sub-directory of "mypack"

ya they are public

In your 6th line of AOTest.java.
try using like this

p1.AOperation a=new p1.AOperation();

If this also wont work then something wrong in AOperation.java file..try recompiling it by deleting all generated classes of P1

Your code works but i want to import all classes

Then try this

javac -cp <P1 pkg path having classes> AOTest.java

Still its not working


C:\Users\nHulk\java>javac -cp C:\Users\nHulk\java\p1 AOTest.java
AOTest.java:1: package p1 does not exist
import p1.*;
^
AOTest.java:6: cannot access AOperation
bad class file: C:\Users\nHulk\java\p1\AOperation.class
class file contains wrong class: p1.AOperation
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
AOperation a=new AOperation();
^
2 errors

The classpath should point to the folder that contains the package path folder.
The classpath plus the package path should point to the folder with the .class file.
There should be no overlap between the two paths.

C:\Users\nHulk\java\p1\
The classpath in red, the package in green

your code is not working

Whose code? What code?

javac -cp <P1 pkg path having classes> AOTest.java

is not working ?

<P1 pkg path having classes>

What you posted is NOT a valid path on Windows.
What OS is that a valid path on?
You must replace <P1 pkg path having classes> with a valid path to the folder containing the p1 folder. In your earlier example, that would be to the java folder.

If you don't show us the layout of the folders you are using, the contents of the folders and the current directory where you issue the java command, this is a big waste of time.

You must tell us exactly where the files are.

C:\Users\nHulk\java : Its contains pakages and java files and class files

C:\Program Files\Java\jdk1.6.0_21\bin : this is bin location

Are ALL of the .class files are in the java folder?

The location of the java or javac commands does not look like it is not a problem.

If there are no packages you should not need a classpath.

Problem is
1) At this location C:\Users\nHulk\java\p1 i have 2 class files AOperation.class & p1class.class

2)i have java file at location C:\Users\nHulk\java with name AOTest.java ,Int this file i am importing all class from package p1 using statement
import p1.*

But while compiling
javac AOtest.java : this statement is giving error

if i use "import p1.AOperation;" then its working but import p1.* is not working !!!

help !!

this statement is giving error

What error? When you post you should give all the needed information.

I just created two .java files and one folder:packageTest. I put one file in the packageTest and the other file in the folder that contains packageTest.


Microsoft Windows XP [Version 5.1.2600]

D:\JavaDevelopment\Testing\ForumQuestions7>dir TestUsing*
Volume in drive D is Work
Volume Serial Number is 0C24-B38C

Directory of D:\JavaDevelopment\Testing\ForumQuestions7

12/25/2011 01:26 PM 365 TestUsingPackage.class
12/25/2011 01:26 PM 120 TestUsingPackage.java
2 File(s) 485 bytes
0 Dir(s) 6,496,976,896 bytes free

D:\JavaDevelopment\Testing\ForumQuestions7>dir packageTest
Volume in drive D is Work
Volume Serial Number is 0C24-B38C

Directory of D:\JavaDevelopment\Testing\ForumQuestions7\packageTest

12/25/2011 01:24 PM <DIR> .
12/25/2011 01:24 PM <DIR> ..
12/25/2011 01:24 PM 270 TestPgm.class
12/25/2011 01:24 PM 72 TestPgm.java
2 File(s) 342 bytes
2 Dir(s) 6,496,976,896 bytes free

D:\JavaDevelopment\Testing\ForumQuestions7>javac TestUsingPackage.java

D:\JavaDevelopment\Testing\ForumQuestions7>

// Import the test package

import packageTest.*;

public class TestUsingPackage {
  TestPgm tp = new TestPgm();
}

The following is in the packageTest folder:

// Test using package

package packageTest;

public class TestPgm {}

Its So Easy i found out the solution

After compiling of packages we have to place them (.java containing packges) in any other folder,
then compile the file which is importing those packages

i.e should place packages AOperation.java and p1class.java in other folder after compiling,not in current working directory

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.