简体   繁体   English

java编译器如何找到没有头文件的类?

[英]How does the java compiler find classes without header files?

当我们在jar中引用类className时,如果没有头文件(比如在c/c++ ),它是如何知道它是否被定义的?

When you run the Java compiler or your application itself, you can specify a classpath which lists all the jars and directories you're loading classes from. 当您运行Java编译器或应用程序本身时,您可以指定一个类路径 ,列出您正在加载类的所有jar和目录。 A jar just contains a bunch of class files; 一个jar只包含一堆类文件; these files have enough metadata in them that no extra header files are necessary. 这些文件中有足够的元数据,不需要额外的头文件。

Java works with classloaders . Java适用于类加载器 Classes are needed for compilation, since it will perform static type checking to ensure that you are using the correct signatures of every method. 编译需要类,因为它将执行静态类型检查以确保您使用每个方法的正确签名。

After compiling them, though, they are not linked like you have in a C/C++ compiler so basically every .class file is standalone. 但是,在编译它们之后,它们没有像在C / C ++编译器中那样链接,所以基本上每个.class文件都是独立的。 Of course this means that you will have to provide compiled classed used by your program when you are going to execute it. 当然,这意味着当您要执行它时,您必须提供程序使用的编译类。 So it's a little bit different from how C and C++ prepare executables. 所以它与C和C ++如何准备可执行文件略有不同。 You don't actually have a linking phase at all, it is not needed. 实际上根本没有链接阶段,不需要它。

The classloader will dinamically load them by adding them to the runtime base used by the JVM. 类加载器将通过将它们添加到JVM使用的运行时基础来动态加载它们。

Actually there are many classloaders that are used by the JVM that have different permissions and properties, you can also invoke it explicitly to ask for a class to be loaded. 实际上,JVM使用的许多类加载器具有不同的权限和属性,您也可以显式调用它以请求加载类。 What happens can also be a sort of "lazy" loading in which the compiled .class code is loaded just when needed (and this loading process can throw a ClassNotFoundException if the asked class is not inside the classpath) 发生的事情也可能是一种“懒惰”加载,其中编译的.class代码在需要时加载(如果被问及的类不在类路径中,则此加载过程可能抛出ClassNotFoundException

The classes in the jar file contain all the required information (class names, method signatures etc) so header files are not needed. jar文件中的类包含所有必需的信息(类名,方法签名等),因此不需要头文件。

When you compile multiple classes javac is clever enough to compile dependencies automatically so the system still works. 当您编译多个类时, javac足够聪明,可以自动编译依赖项,因此系统仍然有效。

它查看类路径并尝试从那里加载类以获取其定义。

Java files are compiled into class files which are java bytecode. Java文件被编译成类文件,这些文件是java字节码。 These class files reside in a file structure where the top level is pointed to by the classpath variable. 这些类文件驻留在一个文件结构中,其中顶级由classpath变量指向。 Compiling in C/C++ creates object files which can be linked into executable binaries. 在C / C ++中编译会创建可以链接到可执行二进制文件的目标文件。 Java only compiles into bytecode files which are pulled in by the JVM at runtime. Java仅编译为字节码文件,这些文件在运行时由JVM引入。 The following provide more explanation. 以下提供更多解释。

http://en.wikipedia.org/wiki/Java_bytecode http://en.wikipedia.org/wiki/Java_bytecode

http://en.wikipedia.org/wiki/Java_compiler http://en.wikipedia.org/wiki/Java_compiler

http://en.wikipedia.org/wiki/Java_Virtual_Machine http://en.wikipedia.org/wiki/Java_Virtual_Machine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM