javac 的类路径和源路径选项之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2441760/
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-08-13 07:42:14  来源:igfitidea点击:

Differences between classpath and sourcepath options of javac

javacommand-linejavac

提问by Alex

I read the Sun documentation and a lot of posts on Stack Overflow, but I'm still confused about the differences between the Java compiler options -cpand -sourcepath.

我阅读了 Sun 文档和 Stack Overflow 上的很多帖子,但我仍然对 Java 编译器选项-cp-sourcepath.

Let say I have this directory structure:

假设我有这个目录结构:

c:\Java\project1\src (where the Java source files are)  
c:\Java\project1\bin (where the Java class files will be or already are)  

Let's also say I have a source file MainClass.javain a package com.mypackage, and that the directory structure is ok in the source folder.

假设我MainClass.java在 package 中有一个源文件com.mypackage,并且源文件夹中的目录结构正常。

I'm in the project1directory and run:

我在project1目录中并运行:

javac -d bin -sourcepath src src/com/mypackage/MainClass.java  

or

或者

javac -d bin -classpath src src/com/mypackage/MainClass.java  

and I obtain the same result. In verbose mode, the search path for source files is srcin both cases.

我得到了相同的结果。在详细模式下,源文件的搜索路径src在两种情况下都是如此。

It would be great if anybody could help me figure out the specifics of these options.

如果有人能帮我弄清楚这些选项的细节,那就太好了。

采纳答案by stacker

 -classpath classpath

Set the user class path, overriding the user class path in the CLASSPATH environment variable. If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory.

设置用户类路径,覆盖 CLASSPATH 环境变量中的用户类路径。如果 CLASSPATH 或 -classpath 均未指定,则用户类路径由当前目录组成。

If the -sourcepath option is not specified, the user class path is searched for source files as well as class files.

如果未指定 -sourcepath 选项,则会在用户类路径中搜索源文件和类文件。

-sourcepath sourcepath
-sourcepath sourcepath

Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.

指定用于搜索类或接口定义的源代码路径。与用户类路径一样,源路径条目由分号 (;) 分隔,可以是目录、JAR 档案或 ZIP 档案。如果使用包,则目录或存档中的本地路径名必须反映包名。

Note that classes found through the classpath are subject to automatic recompilation if their sources are found.

请注意,如果找到源,通过类路径找到的类会自动重新编译。

回答by Kartoch

  • sourcepath is where is the root of your code to compile
  • classpath can contains your code but also the libraries you need
  • sourcepath 是要编译的代码的根目录
  • 类路径可以包含您的代码,也可以包含您需要的库

回答by duffymo

CLASSPATH tells the compiler and the class loader where to look for the .class files it needs.

CLASSPATH 告诉编译器和类加载器在哪里寻找它需要的 .class 文件。

Sourcepath is something I don't use so much. I believe it's optional, because usually the current directory is the sourcepath. CLASSPATH is not.

Sourcepath 是我不太常用的东西。我相信它是可选的,因为通常当前目录是源路径。类路径不是。

回答by Niko Bellic

  • classpath is searched for class (.class) files
  • sourcepath is searched for source (.java) files (.a.k.a class or interface definitions)
  • 在类路径中搜索类 (.class) 文件
  • sourcepath 搜索源 (.java) 文件(.aka 类或接口定义

However, if sourcepath is NOT specified, the classpath is searched for both class files AND source files.

但是,如果未指定 sourcepath,则会在类路径中搜索类文件和源文件。

This leads me to believe that we can almost always keep things simple by using just classpath, and by avoiding sourcepath altogether.

这让我相信我们几乎总是可以通过仅使用类路径并完全避免使用源路径来使事情变得简单。

People who need to use both classpath and sourcepath are probably targeting strange directory contents. For example, they might have a folder that contains source files and class files, but only want to have the source files searched for.

需要同时使用 classpath 和 sourcepath 的人可能针对的是奇怪的目录内容。例如,他们可能有一个包含源文件和类文件的文件夹,但只想搜索源文件。