java 未找到包;爪哇

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3041543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:04:20  来源:igfitidea点击:

Package not found; javac

javajavac

提问by neebz

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.

Main.java 导入SomeOtherPackage。并且两个 java 文件都使用 lib 中的 jars。

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

我所做的是在 CLASSPATH 中独立添加 jar 文件。然后运行为:javac packageName/Main.java

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

但它给出了 Package not found 的错误SomeOtherPackage。它不应该自动实现依赖并构建SomeOtherPackage吗?上述情况的 javac 命令和类路径是什么?

Thanks

谢谢

采纳答案by BalusC

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

通常的做法是将包根添加到类路径中。

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

当您已经在包根目录中时,请使用-cp .. 例如

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:

如果您还想包含 JAR 文件,请使用;(或在 *nix 中的:)作为类路径路径分隔符:

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.

为了节省重复输入所有 shell 命令的时间,请使用.bat(或在 *nix a 中.sh)文件。或者只是一个 IDE,如果您已经熟悉 java/javac 等。

回答by Chris Dodd

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

您需要将 packageName 添加到 CLASSPATH 以便它可以找到 SomeOtherPackage