java import “cannot find symbol”

后端 未结 2 1318
Happy的楠姐
Happy的楠姐 2021-01-24 08:24

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 cla

相关标签:
2条回答
  • 2021-01-24 08:44

    I think you need to compile properly the class bar before attempting to compile foo, this is, generate the class file in the proper package structure.

    0 讨论(0)
  • 2021-01-24 08:48

    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.)

    0 讨论(0)
提交回复
热议问题