简体   繁体   English

使用命令行javac“找不到符号”错误

[英]javac “cannot find symbol” error with command line

I have two classes Owning and OwningAccessor. 我有两个班级Owning和OwningAccessor。 The files are in the same directory. 这些文件位于同一目录中。

public class Owning {
    String _name = "";
    public void printBanner()
    {
    }
    public void printOwning(double amount)
    {
        printBanner();

        //print details
        System.out.println("name:" + _name);
        System.out.println("amount:" + amount);
    }
}


public class OwningAccessor {
    public void access()
    {
        Owning o = new Owning();
        o.printOwning(500);
    }
}

When I tried to compile OwningAccessor with javac -cp . OwningAccessor.java 当我尝试使用javac -cp . OwningAccessor.java编译OwningAccessorjavac -cp . OwningAccessor.java javac -cp . OwningAccessor.java , I got compilation error. javac -cp . OwningAccessor.java ,我收到了编译错误。

symbol  : class Owning
location: class smcho.OwningAccessor
        Owning o = new Owning();
        ^
OwningAccessor.java:6: cannot find symbol
symbol  : class Owning
location: class smcho.OwningAccessor
        Owning o = new Owning();
                   ^

What's wrong with this? 这有什么问题? The code compiles fine under eclipse IDE. 代码在eclipse IDE下编译得很好。

Ok, let's suppose you have the code distributed in files as follows 好的,我们假设您将代码分布在文件中,如下所示

myproject
├── out
└── src
    ├── OwningAccessor.java
    └── Owning.java

Go to your command prompt, and change directory to myproject . 转到命令提示符,将目录更改为myproject Once there issue the following command: 一旦发出以下命令:

javac -d out -sourcepath src src/OwningAccessor.java

I just tested it and it works just fine. 我只是测试它,它工作得很好。 Your compiled classes will be located in the out folder: 您编译的类将位于out文件夹中:

.
├── out
│   ├── OwningAccessor.class
│   └── Owning.class
└── src
    ├── OwningAccessor.java
    └── Owning.java

Compiling one class will trigger the compilation of all other dependent classes. 编译一个类将触发所有其他依赖类的编译。 The compiler will automatically look for them in the src folder. 编译器将自动在src文件夹中查找它们。

Make sure you compile both Owning.java and OwningAccessor.java, like so: 确保编译Owning.java和OwningAccessor.java,如下所示:

javac -cp . Owning.java OwningAccessor.java

Eclipse compiles all necessary files for you, which is why does work there. Eclipse为您编译所有必需的文件,这就是为什么在那里工作。

Try to make a correct sourcepath example: 尝试制作正确的源路径示例:

javac -d temp -sourcepath c:\awork\JavaProjects\singleton\src\ c:\JavaProjects\singleton\src\com\company\MySingleton.java

javac -d temp -sourcepath c:\awork\JavaProjects\singleton\src\ c:\JavaProjects\singleton\src\com\company\Main.java

In "temp" we alocate resources and with -sourcepath indicate where are the .java files. 在“temp”中我们分配资源,而-sourcepath指示.java文件在哪里。

So, in a directory named D:\\Automation there is a file Demo.java throwing this error, in cmd while you are in D:\\Automation, you need to : - 1) cd.. //will pull you out from Automation directory. 因此,在名为D:\\ Automation的目录中,有一个文件Demo.java抛出此错误,当您在D:\\ Automation时,在cmd中,您需要: - 1)cd .. //将从Automation中拉出来目录。 In D:> 2) javac Automation\\Demo.java 在D:> 2)javac Automation \\ Demo.java

this will compile your file - Demo.java 这将编译您的文件 - Demo.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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