Trendingat

02Home & Garden · Updated

How to Read a JAR File: View Its Contents or Code

Short answer

Open it as an archive or inspect its Java classes

A JAR file is a Java archive that can contain program files, resources, and metadata. Use an archive utility to view its contents, or use Java tools to inspect class files and run an executable JAR when you trust its source.

File typeJava archive based on ZIPExplained in: Step-by-Step
View contentsUse an archive utilityExplained in: Step-by-Step
Run safelyTrust the source firstExplained in: Step-by-Step
Reading time6 min7 sections, about 1,480 words

What You’ll Need

  • A computer with the JAR file saved locally.
  • An archive utility that can open ZIP files. Many computers have one built in; tools such as 7-Zip or the macOS Archive Utility can also open JAR files.
  • If you want to run the file or inspect Java bytecode, a Java Development Kit (JDK) is useful. To run a compatible JAR, a Java Runtime Environment or JDK may be required, depending on your system.
  • For command-line steps, use Terminal on macOS or Linux, or Command Prompt or PowerShell on Windows. Menu names and commands can vary slightly by version.

Step-by-Step

  1. Decide what “read” means. If you want to see which files are packed inside a JAR, browse it as an archive. If you want to understand how a Java program works, you will need to inspect its class files, which contain bytecode rather than ordinary readable source code. If you want to use the program, you may need to run it instead. These are different tasks.
  2. Check the file and its source. Confirm the file ends in .jar and came from a source you trust. A JAR can contain executable code. Do not run an unexpected attachment or a file from an unknown website just to see what is inside. Viewing the archive listing is safer than launching it, but use reputable software and avoid opening suspicious contents.
  3. Open it with an archive utility. On Windows, right-click the file and look for an option such as Open with, then choose an archive tool or a ZIP-capable utility. On macOS, try opening it with an archive utility; if it launches or gives an error instead, use a dedicated archive manager. On Linux, use the file manager’s archive option or a ZIP-compatible tool. Menu wording differs by version. If the system treats the JAR as a program, do not double-click it; choose an archive tool explicitly.
  4. Browse the file list. Look for folders and entries such as META-INF, .class files, images, configuration files, or language resources. A listing shows what is packaged but does not make the Java code appear as normal text. The META-INF/MANIFEST.MF file may contain information about the archive, including a main class if it is configured as an executable JAR.
  5. Extract only if needed. To read a text resource or examine files individually, use the archive utility’s Extract option and choose a new, easy-to-find folder. Keep the original JAR unchanged. Extracting files does not convert Java class files into source code, and you should not run extracted programs or scripts unless you trust them.
  6. List contents from a command line. If a JDK is installed, open a terminal in the folder containing the JAR and enter jar tf filename.jar, replacing filename.jar with the actual name. The command prints the archive’s paths without extracting them. You can also use unzip -l filename.jar on systems with the unzip utility. If the command is not recognized, the tool may not be installed or available on your PATH.
  7. Read a text file inside the JAR. Extract the archive first, then open a file ending in .txt, .xml, or .properties with a plain-text editor. Some files may use a character encoding your editor does not detect automatically. Do not try to read a .class file in a text editor and interpret the garbled characters as source code.
  8. Inspect Java class files if required. Java source files usually end in .java; compiled files end in .class. A bytecode viewer or Java decompiler can translate some class files into source-like text, but the result may be incomplete, renamed, or difficult to follow. Use a reputable tool and follow your organization’s rules and the software’s license. If the JAR includes source files, look for .java entries before using a decompiler.
  9. Run an executable JAR only when appropriate. First confirm that you trust the file and need to execute it. With a suitable Java installation, the general command is java -jar filename.jar. Run it from the folder containing the file, using its exact name. Some JARs are libraries and are not standalone programs; they may have no configured main class. Running a JAR can change files or access system resources, so do not use this command as a way to inspect an unfamiliar file.

Common Mistakes

  • Assuming every JAR is an app: A JAR is a container. It may be a library for another program, a collection of resources, or an executable application.
  • Expecting source code in a class file: A .class file contains compiled bytecode. It is not the same as the original .java file.
  • Double-clicking to inspect: Depending on system settings, double-clicking may try to run the JAR. Use an archive utility to browse it instead.
  • Changing the extension to ZIP: JAR uses ZIP packaging, but renaming is usually unnecessary. Open it directly with an archive utility; renaming does not make its contents safer or more readable.
  • Confusing listing with extraction: A command such as jar tf displays names only. Use an archive utility’s extraction option or a suitable extraction command to access the files themselves.

Tips

  • Make a copy before experimenting with extraction, editing, or repackaging.
  • Search the archive listing for README, LICENSE, or a manifest. These may explain what the file is for, though not every archive includes them.
  • Use the file manager’s Properties or Get Info view to check the full filename and file size. A mistaken extension can make an unrelated file look like a JAR.
  • If you only need to confirm that the archive opens, list its contents rather than running it.
  • For Java development, work from source code when available. Decompilation is a fallback, not a guaranteed way to recover the original source.

Troubleshooting

  • “Cannot open” or “unsupported format”: Confirm the file downloaded completely and really is a JAR. Try a reputable ZIP-compatible archive utility. If it still fails, the file may be damaged or may not be a JAR despite its extension; obtain a fresh copy from its trusted source.
  • The jar command is not found: Install a JDK from a trusted distributor, or use an archive utility instead. A Java installation may exist without the JDK command-line tools, and the JDK’s location may not be on the system PATH.
  • java -jar says there is no main manifest attribute: The archive may not be configured as a standalone application. Browse it as an archive or follow the software’s documentation; do not assume that adding a main class yourself is appropriate.
  • “Unable to access jarfile” appears: Check that the terminal is in the correct folder, the filename is spelled exactly, and the file is readable. If the path contains spaces, put the full path in quotation marks.
  • Class files look unreadable: That is expected when opening bytecode as plain text. Look for source or text resources, or use a bytecode viewer or decompiler if you have a legitimate reason to inspect the code.

Frequently Asked Questions

Can I open a JAR file without Java?

Yes. You can usually browse or extract its contents with a ZIP-compatible archive utility without Java. Java is needed to run a compatible JAR, not simply to view its file list.

Is a JAR file the same as a ZIP file?

A JAR uses ZIP archive packaging, with conventions for Java classes and metadata. That is why many ZIP utilities can open it, but a JAR may also be designed to run as a Java application.

Can I read the original Java code in a JAR?

Not necessarily. The archive may include .java source files, but it commonly contains compiled .class files instead. A decompiler can produce a source-like view, but it may not reproduce the original code exactly.

How do I know whether a JAR can run?

Its manifest may identify a main class, but the program can also depend on other files or a particular Java version. Check the documentation and source before running it; a file listing alone cannot establish that it is safe.

Is it safe to open a JAR file?

Viewing its contents with an archive utility is different from executing it, but do not assume an unfamiliar file is harmless. Only run a JAR from a source you trust and when you understand why you need to run it.

Further reading: Oracle Java Documentation. Details can change, so check the official page for anything that matters to you. Spotted a mistake? Tell us. How we write an answer.