javac 的类路径和源路径选项之间的差异 [英] Differences between classpath and sourcepath options of javac

查看:23
本文介绍了javac 的类路径和源路径选项之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 Sun 文档和 Stack Overflow 上的很多帖子,但我仍然对 Java 编译器选项 -cp-sourcepath 之间的差异感到困惑.

假设我有这个目录结构:

c:Javaproject1src(Java 源文件所在的位置)c:Javaproject1in(Java 类文件将在或已经在的位置)

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

我在 project1 目录中并运行:

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

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

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

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

解决方案

 -classpath classpath

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

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

<块引用>

-sourcepath 源路径

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

请注意,通过类路径找到的类如果找到了它们的源,则会自动重新编译.

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 -cp and -sourcepath.

Let say I have this directory structure:

c:Javaproject1src (where the Java source files are)  
c:Javaproject1in (where the Java class files will be or already are)  

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

I'm in the project1 directory and run:

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 src in both cases.

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

解决方案

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

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

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

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

这篇关于javac 的类路径和源路径选项之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆