import class file in java [closed]

2019-03-20 05:17发布

问题:

I have a main directory (contains main.java) and a subdirectory( contains child.java).

My problem is how to instantiate child.java in main.java

  1. I have made the child class public. & added the line#1 as package mypackage
  2. I have compiled child.class with javac -d . child.java which creates a new mypackage directory.
  3. I tried to import child class in main as follows: import subdirectory.mypackage.* (note -d option places the child.class inside mypackage folder)
  4. I compiled the main.java file with "javac main.java"

I get the following error:

mainAESE.java:9: cannot access subdirectory.child
bad class file: RegularFileObject[./subdirectory/child
class file contains wrong class: mypackage.child
Please remove or make sure it appears in the correct subdirectory of the class
child childInstance= new child();
^
1 error

please help me!!

回答1:

Be ensure that the package folder mypackage and Main.class share the parent folder.

package mypackage;
public class Child {}

I presume that the Main class is created in default package.

public class Main {
   public static void main(String []args){
         mypackage.Child child=new mypackage.Child();
   }
}

and your directory structure should be:

main-directory/
              |
              |----/mypackage/
                            Child.class
              |
              | Main.class
              | Main.java
              | Child.java

and to launch/load the Main issue following command,

java Main



回答2:

You need to do two things.

  1. Replace your current import with import mypackage.child

    The import needs to reflect the fully qualified name of the class(es) you're importing. It has nothing to do with the location of the class files on your machine. You could also do import mypackage.* to import all classes from package mypackage instead of specifying the one(s) you want individually... that's just a coding style choice.

  2. Add subdirectory to your classpath

    The classpath, on the other hand, does have to do with the location of class files on your machine. As the name implies, it lists the paths to all of the places where the JVM should look for classes that your program uses. Or, as Oracle puts it, "The class path tells JDK tools and applications where to find third-party and user-defined classes." You don't need to tell Java where to find the special classes that it comes with (like String).

    The instructions for #2 depend on how you're running your program (from the command line or Eclipse or something else). Since you're using the command line (per your comment), you'll need to use the -classpath flag, or its shorthand, -cp, like so: java -cp ./subdirectory main



回答3:

You have the following files:

main.java mypackage/child.java

in main.java, the first line should be:

import mypackage.child;

from the directory where main.java is, run

javac mypackage/child.java

and then:

javac main.java

mypackage should contain child.java and child.class

java main should work, because it will look for child in a subdirectory called mypackage.