Package not found; javac

2019-01-26 17:25发布

This is annoying.

I have a directory structure like this

-lib
   --some jar files

-packageName
   --Main.java
   --SomeOtherPackage
      --SomeOtherJavaClass.java

Main.java imports SomeOtherPackage. And both java files uses jars in the lib.

What I do is add the jar files independently in the CLASSPATH. And then run as: javac packageName/Main.java

but it gives the error that Package not found SomeOtherPackage . Shouldn't it automatically realize the dependency and build SomeOtherPackage as well? What would be the javac command and the classpath for the above case?

Thanks

标签: java javac
2条回答
Bombasti
2楼-- · 2019-01-26 18:05

The normal practice is to add the package root to the classpath.

When you're already in the package root, use -cp .. E.g.

cd /path/to/all/packages
javac -cp . packageName/Main.java

If you want to include JAR files as well, use the ; (or in *nix, the :) as classpath path separator:

javac -cp .;lib/file.jar packageName/Main.java

To save the time in repeating all the typing of shell commands, use a .bat (or in *nix a .sh) file. Or just an IDE if you're already familiar with java/javac and so on.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-26 18:13

You need to add packageName to the CLASSPATH so it can find SomeOtherPackage

查看更多
登录 后发表回答