简体   繁体   中英

Import own class file in .java

Hey an question i have this compiled .class file that contains in the .java file this:

package my.mypackage;
public class MyClass {
//SPACE START
    public static void space(int spacecount) {
    int spacepos = 1;
        while (spacepos <= spacecount) {
            System.out.println("");
            spacepos++;
        }
    }
//SPACE END
//HASH START
    public static int encrypt(String pass) {
        int total = 0;
        int  countone = 0;
        int  counttwo = 0;
        String charlist = "abcdefghiklmnopqrstuvwxyz";
        for (int l = 0; l < pass.length(); l++) {
            countone = pass.charAt(l);
            counttwo = (charlist.indexOf(countone));
            counttwo++;
            total *= 17;
            total += counttwo; 
        }
        return total;
    }
//HASH END
}

and i want to to import it into an other file named a.java it contains this:

import my.mypackage.MyClass;
import java.util.Scanner;
public class a {
    public static void main(String[] args) {
        MyClass.space(4);
        Scanner in = new Scanner();
        System.out.println("Input pass: ");
        String a = in.nextLine();
        int b = MyClass.encrypt(a);
        MyClass.space(4);
        System.out.println(b);

    }
}

both are in one folder. but when im trying to compile it it shows me this:

a.java:1: error: package my.mypackage does not exist                                                                                                                                  
import my.mypackage.MyClass;                                                                                                                                                          
                   ^                         
a.java:5: error: cannot access MyClass                                                                                                                                                
                MyClass.space(4);                                                                                                                                                     
                ^                                                                                                                                                                     
  bad class file: ./MyClass.class                                                                                                                                                     
    class file contains wrong class: my.mypackage.MyClass                                                                                                                             
    Please remove or make sure it appears in the correct subdirectory of the classpath.                                                                                               
2 errors           

can anyone show me how to bring it together step by step ?

Here is the culprit:

both are in one folder.

Java compiler relies on a convention of naming source files and folders in a way that lets it find the source code by examining only the names of files, ie without checking their content; package name is part of the naming convention. That is why the compiler expects to find MyClass.java in a different folder.

Since class MyClass is in the my.mypackage package, while class a is in the default package, their .java files must not be placed in the same folder. Instead, MyClass.java file needs to be placed in the my/mypackage/ (on Windows, my\\mypackage\\ ) folder in relation to the a.java file's location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM