[英]Import own class file in .java
Hey an question i have this compiled .class file that contains in the .java file this: 嘿,一个问题,我有这个编译.class文件,包含在.java文件中:
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: 我想将它导入另一个名为a.java的文件,它包含:
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; Java编译器依赖于命名源文件和文件夹的约定,它允许通过仅检查文件名来查找源代码,即不检查其内容; package name is part of the naming convention.
包名称是命名约定的一部分。 That is why the compiler expects to find
MyClass.java
in a different folder. 这就是为什么编译器希望在不同的文件夹中找到
MyClass.java
原因。
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. 由于
class MyClass
位于my.mypackage
包中,而class a
位于默认包中,因此它们的.java
文件不能放在同一个文件夹中。 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. 相反,
MyClass.java
文件需要放在my/mypackage/
(在Windows上, my\\mypackage\\
)文件夹中,与a.java
文件的位置有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.