import class file in java
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.
-
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 packagemypackage
instead of specifying the one(s) you want individually... that's just a coding style choice. -
Add
subdirectory
to your classpathThe 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.
Rookie
Updated on July 25, 2022Comments
-
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
- I have made the child class public. & added the line#1 as
package mypackage
- I have compiled
child.class
withjavac -d . child.java
which creates a new mypackage directory. - I tried to import child class in main as follows:
import subdirectory.mypackage.*
(note -d option places the child.class inside mypackage folder) - 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!!
- I have made the child class public. & added the line#1 as
-
Pops over 11 years@Rookie sorry, just fixed a typo. "child" and "class" look too similar this late at night.
-
Rookie over 11 yearsI tried your solution but i get this error: <identifier> expected import mypackage.class; ^ 1 error
-
Rookie over 11 yearsmy 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 over 11 yearsIt'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 over 11 yearshello.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 over 11 yearsWhat is the package declaration of
child
? -
Rookie over 11 yearspackage declaration of child is 'package AESE'
-
Pops over 11 yearsYour 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.