import class file in java

81,908

Solution 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

Solution 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

Solution 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.

Share:
81,908
Rookie
Author by

Rookie

Updated on July 25, 2022

Comments

  • Rookie
    Rookie almost 2 years

    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!!

  • Pops
    Pops over 11 years
    @Rookie sorry, just fixed a typo. "child" and "class" look too similar this late at night.
  • Rookie
    Rookie over 11 years
    I tried your solution but i get this error: <identifier> expected import mypackage.class; ^ 1 error
  • Rookie
    Rookie over 11 years
    my folder structure does not have the package folder and main.class in the same folder.. as I said, package folder is within a subdirecory,what do i do in this case?
  • Pops
    Pops over 11 years
    It's unclear from your question whether your main class is actually named "main" or "mainAESE"; it's similarly unclear whether your subdirectory is actually called "subdirectory" or not. You may have to make some adjustments to my answer to account for things like that. Are you getting the error even after adjusting?
  • Rookie
    Rookie over 11 years
    hello.my main class is actually mainAESE.java (i wrote main class for simplicity at SO).. my subdirectory is actually ./aes_wrapper/AESE/ which has child.java. I tried your answer, and am having difficulty adding the class path using 'cp'..even after adjusting
  • Pops
    Pops over 11 years
    What is the package declaration of child?
  • Rookie
    Rookie over 11 years
    package declaration of child is 'package AESE'
  • Pops
    Pops over 11 years
    Your package declaration ought to match your directory structure. Currently it would have to be package aes_wrapper.AESE.child to match. But you could also change the location where the file is saved, whichever is easiest for you.