Как изменить jar файл
Перейти к содержимому

Как изменить jar файл

  • автор:

Updating a JAR File

The Jar tool provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.

The basic command for adding files has this format:

In this command:

  • The u option indicates that you want to update an existing JAR file.
  • The f option indicates that the JAR file to update is specified on the command line.
  • jar-file is the existing JAR file that is to be updated.
  • input-file(s) is a space-delimited list of one or more files that you want to add to the JAR file.

Any files already in the archive having the same pathname as a file being added will be overwritten.

When creating a new JAR file, you can optionally use the -C option to indicate a change of directory. For more information, see the Creating a JAR File section.

Examples

Recall that TicTacToe.jar has these contents:

Suppose that you want to add the file images/new.gif to the JAR file. You could accomplish that by issuing this command from the parent directory of the images directory:

The revised JAR file would have this table of contents:

You can use the -C option to "change directories" during execution of the command. For example:

Как отредактировать содержимое .jar файла на Mac

wikiHow работает по принципу вики, а это значит, что многие наши статьи написаны несколькими авторами. При создании этой статьи над ее редактированием и улучшением работали авторы-волонтеры.

Количество просмотров этой статьи: 6157.

У вас когда-нибудь возникала необходимость отредактировать содержимое .jar файла на Mac? Что ж, эта статья для вас! Вы научитесь редактировать содержимое .jar файла на Mac без всяких причудливых приложений!

# Bytecode Modification

Bytecode is the set of instructions used by the JVM. To illustrate this let’s take this Hello World program.

This is what it turns into when compiled into bytecode.

# What’s the logic behind this?

getstatic — Retreives the value of a static field of a class. In this case, the PrintStream "Out" of System.

ldc — Push a constant onto the stack. In this case, the String "Hello World"

invokevirtual — Invokes a method on a loaded reference on the stack and puts the result on the stack. Parameters of the method are also taken from the stack.

# Well, there has to be more right?

There are 255 opcodes, but not all of them are implemented yet. A table with all of the current opcodes can be found here: Java bytecode instruction listings

# How can I write / edit bytecode?

There’s multiple ways to write and edit bytecode. You can use a compiler, use a library, or use a program.

# I’d like to learn more about bytecode!

There’s probably a specific documentation page specificially for bytecode. This page focuses on the modification of bytecode using different libraries and tools.

# How to modify jars with the ASM library

Firstly the classes from the jar need to be loaded. We’ll use three methods for this process:

  • loadClasses(File)
  • readJar(JarFile, JarEntry, Map)
  • getNode(byte[])

With these methods loading and changing a jar file becomes a simple matter of changing ClassNodes in a map. In this example we will replace all Strings in the jar with capitalized ones using the Tree API.

Now that all of the ClassNode’s strings have been modified we need to save the changes. In order to save the changes and have a working output a few things have to be done:

  • Export ClassNodes to bytes
  • Load non-class jar entries (Ex: Manifest.mf / other binary resources in jar) as bytes
  • Save all bytes to a new jar

From the last portion above, we’ll create three methods.

  • processNodes(Map<String, ClassNode> nodes)
  • loadNonClasses(File jarFile)
  • saveAsJar(Map<String, byte[]> outBytes, String fileName)

The methods used:

That’s it. All the changes will be saved to "sample-edit.jar".

# How to load a ClassNode as a Class

# How to rename classes in a jar file

SimpleRemapper is an existing class in the ASM library. However it only allows for class names to be changed. If you wish to rename fields and methods you should create your own implemenation of the Remapper class.

# Javassist Basic

Javassist is a bytecode instrumentation library that allows you to modify bytecode injecting Java code that will be converted to bytecode by Javassist and added to the instrumented class/method at runtime.

Lets write the first transformer that actually take an hypothetical class "com.my.to.be.instrumented.MyClass" and add to the instructions of each method a log call.

Now in order to use this transformer (so that our JVM will call the method transform on each class at load time) we need to add this instrumentor this with an agent:

Last step to start our first instrumentor experiment is to actually register this agent class to the JVM machine execution. The easiest way to actually do it is to register it with an option into the shell command:

How can I edit a .jar file? [duplicate]

So I have a jar file with one .class file on it. I just need to change some words in the file. What program should I use?

I want this to work for my phone.

Samuel Liew's user avatar

3 Answers 3

Here’s what I did:

  • Extracted the files using WinRAR
  • Made my changes to the extracted files
  • Opened the original JAR file with WinRAR
  • Used the ADD button to replace the files that I modified

That’s it. I have tested it with my Nokia and it’s working for me.

Fergal's user avatar

Vaibhav's user avatar

A jar file is a zip archive. You can extract it using 7zip (a great simple tool to open archives). You can also change its extension to zip and use whatever to unzip the file.

Now you have your class file. There is no easy way to edit class file, because class files are binaries (you won’t find source code in there. maybe some strings, but not java code). To edit your class file you can use a tool like classeditor.

You have all the strings your class is using hard-coded in the class file. So if the only thing you would like to change is some strings you can do it without using classeditor.

This is a tool to open Java class file binaries, view their internal structure, modify portions of it if required and save the class file back. It also generates readable reports similar to the javap utility. Easy to use Java Swing GUI. The user interface tries to display as much detail as possible and tries to present a structure as close as the actual Java class file structure. At the same time ease of use and class file consistency while doing modifications is also stressed. For example, when a method is deleted, the associated constant pool entry will also be deleted if it is no longer referenced. In built verifier checks changes before saving the file. This tool has been used by people learning Java class file internals. This tool has also been used to do quick modifications in class files when the source code is not available.» this is a quote from the website.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *