imran
imran

Reputation: 11

import packagename.* not working

I have written the following code:

package abc.def;

public class test {

public void test() {}
public void disp() {
 System.out.println("in disp");
}

}

then I used following command to compile:

javac -d . test.java

it works fine, but when I tried to import the class "test" using "import abc.def.*" it does not import test class, the code is :

import abc.def.*;


public class checktest {

 public static void main(String a[]) {

 test t = new test();

}

}

following error is generated:

D:\javaprograms>javac  checktest.java
checktest.java:8: cannot access test
bad class file: .\test.java
file does not contain class test
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
 test t = new test();
 ^
1 error

Upvotes: 1

Views: 2699

Answers (5)

user3260381
user3260381

Reputation:

It seems that you have by mistake compiled test.java in the topmost directory itself, therefore the JVM is picking test.class from the top most directory and also from abc\def\test.class hence conflict is happening.

please type: ls test* in the top most directory and confirm if that is the case and delete this extra test.class and then recompile.

Upvotes: 0

Britto
Britto

Reputation: 31

I also had the same problem.

No additional classpath is required to set.

According to your scenario, your working directory might contains test.java file. You can just remove the test.java file from the working directory and compile using javac checktest.java.

It will work.

Thanks. Britto

Upvotes: 2

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74780

Your directory structure should look like this:

  • current working directory
    • checktest.java
    • abc
      • def
        • test.java

Then, from the directory on the top, you can compile checktest:

  javac checktest.java

This will automatically find (and compile) test.java too. If you only want to compile test, do it this way:

  javac abc/def/test.java

Then all the class files will be in the right directories, too.

Upvotes: 1

ayush
ayush

Reputation: 14568

first know this - To use the package in other programs, compile the .java files as usual and then move the resulting .class files into the appropriate subdirectory of one of the directories referenced in your CLASSPATH environment variable.

For instance if /home/name/classes is in your CLASSPATH and your package is called package1, then you would make a directory called package1 in /home/name/classes and then put all the .class files in the package in /home/name/classes/package1.

Now suppose your classpath is /home/name/classes then compile

package abc.def;

public class test {

public void test() {} public void disp() { System.out.println("in disp"); }

}

using $ javac -d /home/name/classes test.java

Now put this code

import abc.def.*;

public class checktest {

public static void main(String a[]) {

test t = new test();

}

}

inside the folder

Upvotes: -1

prasanna
prasanna

Reputation: 1907

Did you make the proper directory structure? You need to have the test.java file in abc/def if that's the package name you want.

You can also point to the compiled test.class file with -cp flag

Example:

javac -cp test checktest

Upvotes: 1

Related Questions