View class files in java

Java Class File

A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know that a single Java programming language source file (or we can say .java file) may contain one class or more than one class. So if a .java file has more than one class then each class will compile into a separate class files.
For Example: Save this below code as Test.java on your system.

For Compiling:

After compilation there will be 3 class files in corresponding folder named as:

A single class file structure contains attributes that describe a class file.
Representation of Class File Structure

Elements of class file are as follows:

  1. magic_number: The first 4 bytes of class file are termed as magic_number. This is a predefined value which the JVM use to identify whether the .class file is generated by valid compiler or not. The predefined value will be in hexadecimal form i.e. 0xCAFEBABE.
    Now let’s see what happen when JVM will not find valid magic number. Suppose we have a .java file named as Sample.java as follows and follow step by step process on your system.
// class Declaration class Sample < public static void main(String[] args) < System.out.println("Magic Number"); >>

Step 1: Compile using javac Sample.java
Step 2: Now open the Sample.class file. It will looks like following.

Читайте также:  Java io eofexception retrofit

Sample.class File

Runtime Exception Due to Invalid magic number

Step 3: Now erase at least single symbol from this Sample.class file from starting of file and save it.
Step 4: Now try to run this using java Sample command and see the magic i.e. you will get run time exception (See the highlighted text in below image):

Note: This can vary depending on how much you remove the .class file data.

Note: Lower version compiler generated .class file can be executed by high version JVM but higher version compiler generated .class file cannot be executed by lower version JVM. If we will try to execute we will get run time exception.

This demonstration is for Windows OS as follows:

Step 1: Open a command prompt window and try to check java compiler version and JVM version using following commands respectively (Highlighted text in image are the commands)
Output for 1.8 version will be:
Java Compiler Version
JVM Version

Step 2: Now check with another version which may be higher or lower than already installed.thisDownload link.
And install this to your PC or laptops and note the installation address.
Step 3: Open a second command prompt window and set the path of bin folder of installed jdk installed during 2nd step. And check for Java compiler version ad JVM version.
Checking Version of JDK 1.6
Step 4: Now on 1st command prompt compile the any valid .java file. For example: See above Sample.java file. Compile it as:
Compiling with Compiler Version 1.8
Step 5: Now on 2nd command prompt window try to run the above compiled code class file and see what happen. There is a run time exception which I have highlighted in below image.

Runtime Exception Due to Invalid major and minor Version of class file

Note: Internally jdk 1.5 version means 49.0 and 1.6 means 50.0 and 1.7 means 51.0 etc. class file version where the digits before the decimal point represent the major_version and digits after decimal point represents the minor_version.

  1. constant_pool_count: It represents the number of the constants present in the constant pool (When a Java file is compiled, all references to variables and methods are stored in the class’s constant pool as a symbolic reference).
  2. constant_pool[]: It represents the information about constants present in constant pool file.
  3. access_flags: It provide the information about the modifiers which are declared to the class file.
  4. this_class: It represents fully qualified name of the class file.
  5. super_class: It represents fully qualified name of the immediate super class of current class. Consider above Sample.java file. When we will compile it, then we can say this_class will be Sample class and super_class will be Object class.
  6. interface_count: It returns the number of interfaces implemented by current class file.
  7. interface[]: It returns interfaces information implemented by current class file.
  8. fields_count: It represents the number of fields (static variable) present in current class file.
  9. fields[]: It represent fields (static variable) information present in current class file.
  10. method_count: It represents number of methods present in current class file.
  11. method[]: It returns information about all methods present in current class file.
  12. attributes_count: It returns the number of attributes (instance variables) present in current class file.
  13. attributes[]: It provides information about all attributes present in current class file.

Источник

View class files in java

Hello, from the link you provided above, I did not find the code for loading any class. would you please describe the problem more specific again?

I posted Java source code. If my link is inaccessible or broken check another link:
Link[^]

There is several code fixes and enhancements in the tool, and the latest code is here.

I plan to modify the class file , the library has this kind of saving API ?

Currently there is no such API in this library.

Unfortunately there is no such function until now.
This is an read-only library.

I hope you are doing well

If I use your library jCFL how I can read/get the values of the following method attributes the max_stack , max_locals, code_length and code for each method in class file

I’ve created a program «GetMethodAttributes.zip» for your question.
Please download it from the following link.

Please play with it, and any further question is welcome.

Note that, for the method ‘code‘, this program just displays:
a. the raw HEX data of the code, and
b. the JVM opcode.

This library does not have de-compile capability until now.

really I appreciate your effort

I will play with and get back to you soon

I hope you are doing well

If I use your library jCFL how I can read/get the values of the following method attributes the max_stack , max_locals, code_length and code for each method in class file

a new tech that i’ve find earlier is shine pattern

it’s very power ful and easy to use download it from below , it has also a document

General News Suggestion Question Bug Answer Joke Praise Rant Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Источник

Managing Source and Class Files

Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this. The strategy is as follows.

Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java . For example:

//in the Rectangle.java file package graphics; public class Rectangle

Then, put the source file in a directory whose name reflects the name of the package to which the type belongs:

The qualified name of the package member and the path name to the file are parallel, assuming the Microsoft Windows file name separator backslash (for UNIX, use the forward slash).

  • class name – graphics.Rectangle
  • pathname to file – graphics\Rectangle.java

As you should recall, by convention a company uses its reversed Internet domain name for its package names. The Example company, whose Internet domain name is example.com , would precede all its package names with com.example . Each component of the package name corresponds to a subdirectory. So, if the Example company had a com.example.graphics package that contained a Rectangle.java source file, it would be contained in a series of subdirectories like this:

. \com\example\graphics\Rectangle.java

When you compile a source file, the compiler creates a different output file for each type defined in it. The base name of the output file is the name of the type, and its extension is .class . For example, if the source file is like this

//in the Rectangle.java file package com.example.graphics; public class Rectangle < . . . >class Helper

then the compiled files will be located at:

\com\example\graphics\Rectangle.class \com\example\graphics\Helper.class

Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:

\sources\com\example\graphics\Rectangle.java \classes\com\example\graphics\Rectangle.class

By doing this, you can give the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.

The full path to the classes directory, \classes , is called the class path, and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path. For example, if

is your class path, and the package name is

then the compiler and JVM look for .class files in

\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (UNIX). By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in your class path.

Setting the CLASSPATH System Variable

To display the current CLASSPATH variable, use these commands in Windows and UNIX (Bourne shell):

In Windows: C:\> set CLASSPATH In UNIX: % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use these commands:

In Windows: C:\> set CLASSPATH= In UNIX: % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable, use these commands (for example):

In Windows: C:\> set CLASSPATH=C:\users\george\java\classes In UNIX: % CLASSPATH=/home/george/java/classes; export CLASSPATH

Источник

Оцените статью