java报无法找到符号,java import“无法找到符号”

I'm about porting a linux tool to windows. The tool works fine on linux system, but now on windows I get this "cannot find symbol" error.

I have this little main class:

package foo;

import foo.bar;

public class Main {

public static void main(String[] args) throws Exception {

bar.getInstance();

}

}

and the error appears now, while doing javac Main.java:

import foo.bar: cannot find symbol

^

symbol: class bar

location: package foo

Main.java and bar.java are in the same directory.

what am I missing?

解决方案

For one thing, bar should be called Bar to be idiomatic...

Ideally, you should compile from the directory above Main.java, like this:

javac -d out foo/Main.java foo/Bar.java

That will create a directory called "out" containing another directory "foo", which will contain Main.class and Bar.class. So from the parent directory again, you could run:

java -cp out foo.Main

The source locations don't have to match the package structure. You could just call javac from the directory containing Main.java and Bar.java like this:

javac -cp out Main.java Bar.java

(And then run it in the same way as before) However, it's generally a much better idea to structure your source code according to packages.

You may well find it easier to use an IDE (Eclipse or NetBeans, for example) which will handle all the compilation etc for you. If you do want to build a real project from the command line, you should probably look into using a full build system such as Ant or Maven.

(Note that you'd get the same error on Linux as on Windows, if you tried to compile in the same way.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值