Java 包 - 找不到类,同一目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:58:10  来源:igfitidea点击:

Java package - class not found, same directory

java

提问by Sundeep

I have two Java classes "giveMyOb" and "dataConn" declared in the same directory. Both are public classes. "giveMyOb" has a static method "getMine()". Inside dataConn, I called the static method as

我在同一目录中声明了两个 Java 类“giveMyOb”和“dataConn”。两者都是公开课。“giveMyOb”有一个静态方法“getMine()”。在 dataConn 中,我将静态方法称为

giveMyOb.getMine();

When I try to compile dataConn.java, the following error is returned.

当我尝试编译 dataConn.java 时,返回以下错误。

"Cannot find symbol

“找不到标志

symbol: variable giveMyOb
location : class dataConn
giveMyOb.getMine(); "

符号:变量 giveMyOb
位置:类 dataConn
giveMyOb.getMine(); ”

It used to work earlier. But is not working now. Why is that?

它曾经工作得更早。但现在不工作。这是为什么?

Additional Information: JDK 1.6. Windows 7. 64 bit.

附加信息:JDK 1.6。Windows 7。64 位。

Update(30 days after the question): When compiled from Eclipse, the classes are referenced and it works. But the same won't work when compiling from command line. I was unable to figure out the reason and nothing logical comes to my mind!

更新(问题发生后 30 天):从 Eclipse 编译时,类被引用并且可以工作。但是从命令行编译时同样不起作用。我无法弄清楚原因,没有任何合乎逻辑的想法出现在我的脑海中!

回答by duffymo

javac -classpath . *.java

ought to create both .class files at the same time. It's more complicated by packages. I'm assuming you have none.

应该同时创建两个 .class 文件。包更复杂。我假设你没有。

Learn the Sun Java coding conventions. You aren't following them with those class names. They should start with a capital letter.

学习 Sun Java 编码约定。你没有用这些类名来跟随他们。它们应该以大写字母开头。

回答by Bart Kiers

Try this:

试试这个:

giveMyOb.java

给MyOb.java

public class giveMyOb {
    public static String getMine() {
        return "Yay, it works!";
    }
}

dataConn.java

数据连接器

public class dataConn {
    public static void main(String[] args) {
        System.out.println(giveMyOb.getMine());
    }
}

Then compile it all:

然后全部编译:

javac *.java

and run the main class:

并运行主类:

java -cp . dataConn
// output: Yay, it works!

Note that Java's coding conventionsrecommend class names start with a capital.

请注意,Java 的编码约定建议类名以大写开头。

If "it" still doesn't work, try removing the .classfiles manually then recompile again.

如果“它”仍然不起作用,请尝试.class手动删除文件,然后重新编译。