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

查看:330
本文介绍了java编译器如何查找类文件而classpath未设置为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 :

SampleApp

|____**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 :

prompt\SampleApp\src>javac -d ..\classes -sourcepath . pack\Sample.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 / 帮助我理解了很多东西。文章作者说默认的类路径是当前的工作目录。但我不明白为什么我的源代码编译没有错误。有人可以向我解释一下吗?

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 在bootclasspath上找到了class。这是由 javac 命令设置的隐式,用于引用JDK安装中的相关JAR。 javac 命令在查找常规类路径中的内容之前搜索bootclasspath。

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编译器如何查找类文件而classpath未设置为jdk路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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