简体   繁体   中英

How does Java know the methods of an external jar?

What I don't get is how does Java know the methods of a jar that is referenced? If it is compiled just for running and you can't read it I don't see how you can see the methods still. An example of my question is like if you made a jar that makes a box show up on the screen using a method called "ShowABox". And you add it to another Java project. Then how does the IDE know that a method called "ShowABox" exists since the jar was already compiled? You can't read class files in an IDE so why can it read methods?

All the information you are referring to is actually stored in the class files precisely for this reason.

As to seeing the code in class files, you can certainly do so, and it will also prove that the information was kept. Have a look at Java Decompiler . Note you can even build this into eclipse if you want to see it directly there.

Compiled classes contain bytecode. Methods still has their real names, but their code compiled to JVM instructions.

You can read java class file format specification on wiki , read "The constant pool" paragraph, methods names (as other class information) contains in constant pool.

Just try to open some .class file in text editor, you will find methods names there. (.class files are often in project/bin folder, or open .jar as archive and get .class file from there)

A JAR is nothing more than all the class files zipped in a single file with a manifest attached. Each class file completely describes its public interface.

JAR-files have a very specific format — see http://en.wikipedia.org/wiki/JAR_(file_format) — and they contain class-files, which also have a very specific format — see http://en.wikipedia.org/wiki/Java_class_file . This format, in addition to providing the Java Virtual Machine with the information it needs to execute code, also provides IDEs and compilers with the information they need to find classes, interfaces, fields, methods, and so on.

A jar is nothing but an archive containing Java compiled .class binaries compressed for compactness into a single file. Its contents are compiled binaries organized in a directory structure. So you can think of it as a directory with files but compressed into a single archive (just like a zip file). A jar itself is not a binary ("exists since the jar was already compiled") -- it doesn't get compiled itself but it rather contains compiled elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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