java编译器如何找到类文件而类路径未设置为jdk路径? [英] How does the java compiler find the class files whereas the classpath is not set to the jdk path?

查看:35
本文介绍了java编译器如何找到类文件而类路径未设置为jdk路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试深入了解 Java 编译.所以我把我的 IDE 放在一边,开始使用 MS-DOS 命令行......

I'm trying to look under the hood about java compilation. So I put my IDE away and started using MS-DOS command-line...

我创建了一个简单的项目,如下图所示:

I created a simple project, as described in the tree below :

示例应用

|____**src**

       |_____pack
               |______Sample.java
|____**classes**

这是 Sample.java 源代码:

This is the Sample.java source code :

public class Sample 
{

    private String s = new String("Hello, world");

    public Sample(){
          System.out.println(s);
    }
}

我只是想编译这个类,所以我使用了javac命令:

I just want to compile this class, so I used the javac command :

promptSampleAppsrc>javac -d ..classes -sourcepath . packSample.java

一切正常,但我没想到,因为我在编译我的 Sample.java 文件之前删除了我的 CLASSPATH 环境变量.因此,由于编译器无法找到 java.lang.String 类文件,我预计会出现编译器错误.

All works fine, but i didn't expect that because I deleted my CLASSPATH environment variable before compiling my Sample.java file. So I was expecting a compiler error due to the fact that the compiler would not be able to find the java.lang.String class file.

我阅读了这篇文章 http://www.ibm.com/developerworks/java/library/j-classpath-windows/ 这帮助我理解了很多东西.文章作者说默认的classpath是当前工作目录.但我不明白为什么我的源代码编译没有错误.有人可以向我解释一下吗?

I read this article http://www.ibm.com/developerworks/java/library/j-classpath-windows/ which helped me understand many things. The article author says that the default classpath is the current working directory. But I don't understand why my source code compile without error. Could someone explain this to me?

推荐答案

因此,由于编译器无法找到 java.lang.String 类文件,我预计会出现编译错误.

So I was expecting a compiling error due to the fact that the compiler would not be able to find the java.lang.String class file.

简短的回答是编译器知道在哪里可以找到所有标准 Java SE 库类,而无需您告诉它.

The short answer is that the compiler knows where to find all of the standard Java SE library classes without you telling it.

更长的答案是在引导类路径中找到了 String 类.这是由 javac 命令隐式设置的,用于引用 JDK 安装中的相关 JAR.javac 命令在查找常规类路径上的内容之前先搜索引导类路径.

The longer answer is that String class is being found on the bootclasspath. This is implicitly set by the javac command to refer to the relevant JARs in the JDK installation. The javac command searches the bootclasspath before it looks for stuff on the regular classpath.

这篇关于java编译器如何找到类文件而类路径未设置为jdk路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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