javac“找不到符号"命令行错误 [英] javac "cannot find symbol" error with command line

查看:54
本文介绍了javac“找不到符号"命令行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类Owning 和OwningAccessor.文件在同一个目录下.

公共类拥有{字符串 _name = "";public void printBanner(){}public void printOwning(双倍金额){打印横幅();//打印详细信息System.out.println("名称:" + _name);System.out.println("金额:" + 金额);}}公共类拥有访问器{公共无效访问(){拥有 o = new Ownering();o.printOwning(500);}}

当我尝试使用 javac -cp 编译 OwningAccessor 时.拥有Accessor.java,我得到了编译错误.

symbol : 类拥有位置:类 smcho.OwningAccessor拥有 o = new Ownering();^OwneringAccessor.java:6: 找不到符号符号:类拥有位置:类 smcho.OwningAccessor拥有 o = new Ownering();^

这有什么问题吗?代码在eclipse IDE下编译正常.

解决方案

好吧,假设您将代码分发到文件中,如下所示

我的项目├──出└── src├── OwningAccessor.java└── 拥有.java

转到您的命令提示符,并将目录更改为 myproject.在那里发出以下命令:

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

我刚刚测试了它,它工作得很好.您编译的类将位于 out 文件夹中:

<预><代码>.├──出│ ├── OwningAccessor.class│ └── Owning.class└── src├── OwningAccessor.java└── 拥有.java

编译一个类将触发所有其他依赖类的编译.编译器会自动在 src 文件夹中寻找它们.

I have two classes Owning and 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, I got compilation error.

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.

解决方案

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. 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
│   ├── 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.

这篇关于javac“找不到符号"命令行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆