-sourcepath 与 -classpath [英] -sourcepath vs -classpath

查看:40
本文介绍了-sourcepath 与 -classpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为 oracle 认证学习期间,我正在尝试考试期间可能出现的所有可能情况.比如这里对shell命令行有一点疑问(基于unix):

Studying for the oracle certification I am trying all the possible scenarios that might occur during the exam. For example, here there is a little doubt about shell command line (unix based):

假设有一个名为 myProject 的文件夹和一个名为 myProject/source 的子文件夹.

Let's imagine there is a folder called myProject and a sub folder called myProject/source.

文件 SubFile.java 在文件夹 myProject/source 中,另一个文件 File.javamyProject 中> 文件夹.

File SubFile.java is in the folder myProject/source and another file File.java is in myProject folder.

通过输入以下命令,我可以得到不同的行为:

By typing the following commands I get to different behaviors:

cd source(因此,目前我在myProject/source"上)

cd source (therefore, currently I am on "myProject/source")

javac -sourcepath ../File.java
//命令 ../无法访问文件夹"然后在从 myProject 文件夹编译 File.java 并返回到子文件夹后,如果我尝试:

javac -sourcepath ../ File.java
// The command ../ does not work to access "Folder" then after compiling File.java from myProject folder and returning to the sub Folder if I try:

javac -classpath ../SubFile.java

//带有标志 -classpath 它似乎接受 ../ 语法来访问 super 文件夹.

// with the flag -classpath it seems to accept the ../ syntax to access the super folder.

你知道为什么会这样吗?此外,是否有机会使用 -sourcepath 标志访问 super 文件夹?

Do you know why it works like this? and moreover is there any chance to access the super folder with the -sourcepath flag?

推荐答案

这取决于SubFile是否也引用了File.

It depends on whether SubFile also references File.

考虑以下代码:

public class SubFile {
    private static File file = new File();
}

假设这个文件在你的源文件夹中,并假设你在源文件夹中,那么

Assumed that this file is located in your source folder, and assumed that you are in the source folder, then

javac -sourcepath ../ SubFile.java

SubFile.java编译成源码文件夹内的SubFile.class,并将File.java编译成File.class 在父文件夹中.如果这些文件之间没有依赖关系,那么编译器将编译File.java(意味着编译器不会自动编译sourcepath).

will compile SubFile.java into SubFile.class inside the source folder, and will compile File.java into File.class in the parent folder. If there is no dependency between those files, then the compiler will not compile File.java (means, the compiler will not automatically compile all files on the sourcepath).

当用-classpath编译时,那么classpath也会搜索源文件,除非你明确指定了一个单独的源路径——在下面的情况下,编译器会抛出一个错误(假设你之前已经清理过 File.class 文件):

When compiling with -classpath, then the classpath is also searched for source files, unless you explicitly specify a separate sourcepath - in the following case, the compiler will throw an error (assumed that you have cleaned the File.class file before):

javac -classpath .. -sourcepath 	emp SubFile.java

另见 javac - Java 编程语言编译器javac 的 classpath 和 sourcepath 选项之间的区别了解更多信息.

See also javac - Java programming language compiler and Differences between classpath and sourcepath options of javac for more information.

这两个链接的重点是:

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

Note: Classes found through the class path may be subject to automatic recompilation if their sources are also found.

这篇关于-sourcepath 与 -classpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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