是这样的。
有一次,hardcode了ip地址在代码里,由于懒得重新编译。
强行用记事本打开.class文件,发现里面已经面目全非了,但是数字还是原来的样子。
于是直接改了.class文件里的数字,换了个ip地址,结果运行时是直接崩溃的。
为什么呢?是有什么检查机制吗?
-------补充--------------
不好意思 描述不太准确啊
补充个例子
1)我先新建了一个.java
public class Test {
public Test() {
}
public static void main(String[] args) {
String ip = "172.18.81.111";
System.out.println(ip);
}
}
然后 javac Test.java
运行java Test
结果正常
输出 172.18.81.111
2)然后我用vim修改.class文件
Êþº¾^@^@^@4^@^]
^@^F^@^O^H^@^P ^@^Q^@^R
^@^S^@^T^G^@^U^G^@^V^A^@^F<init>^A^@^C()V^A^@^DCode^A^@^OLineNumberTable^A^@^Dmain^A^@^V([Ljava/lang/String;)V^A^@
SourceFile^A^@ Test.java^L^@^G^@^H^A^@^M172.18.81.111^G^@^W^L^@^X^@^Y^G^@^Z^L^@^[^@^\^A^@^DTest^A^@^Pjava/lang/Object^A^@^Pjava/lang/System^A^@^Cout^A^@^ULjava/io/PrintStream;^A^@^Sjava/io/PrintStream^A^@^Gprintln^A^@^U(Ljava/lang/String;)V^@!^@^E^@^F^@^@^@^@^@^B^@^A^@^G^@^H^@^A^@ ^@^@^@^]^@^A^@^A^@^@^@^E*·^@^A±^@^@^@^A^@
^@^@^@^F^@^A^@^@^@^D^@ ^@^K^@^L^@^A^@ ^@^@^@+^@^B^@^B^@^@^@^K^R^BL²^@^C+¶^@^D±^@^@^@^A^@
^@^@^@^N^@^C^@^@^@^H^@^C^@
^@
^@^K^@^A^@^M^@^@^@^B^@^N
~
~
上面的.class文件中明显有一串数字,直接修改之。
然后再运行java Test
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.ClassFormatError: Unknown constant tag 49 in class file Test
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
为什么修改个数字就会崩溃呢?
PHP中文网2017-04-17 17:42:30
In fact, it is possible. . Software like javassist can do it.
Or other hexadecimal editors are also available.
The Notepad file that comes with Windows will no longer work.
Quote from the first floor Remember not to use the Notepad that comes with Windows to open text files!
记住不要用windows自带的记事本打开文本文件!
用ultraedit
打开编辑之后正常运行。
编辑的时候文字长度一定要和原来一样,否则会出现错误。
ultraedit
.
When editing, the text length must be the same as the original, otherwise errors will occur.
With pictures天蓬老师2017-04-17 17:42:30
Remember not to open text files with the Notepad that comes with Windows!
Remember not to open text files with the Notepad that comes with Windows!
Remember not to open text files with the Notepad that comes with Windows!
Don’t change the numbers at will, it is possible that you changed the magic numbers.
This file is obsolete.
巴扎黑2017-04-17 17:42:30
View ClassFormatError documentation https://docs.oracle.com/javase/7/docs/api/java/lang/ClassFormatError.html
translated meansThrown when the Java Virtual Machine attempts to read a class file and
# 🎜🎜#
determines that the file is malformed or otherwise cannot be
interpreted as a class file.
When the Java virtual machine tries to read a class file and finds that the file is malformed or cannot be interpreted as a class file, a ClassFormatError error will be thrown.The error name "ClassFormatError", as the name suggests, is a class file formatting error. Based on the above content, it is guessed that it is an error that occurs when the virtual machine reads the class file and needs to be interpreted and formatted. The specific process is unknown.
PHPz2017-04-17 17:42:30
The class file is a bytecode file generated by javac after lexical analysis, syntax analysis, and semantic analysis of the java source file. Direct modification will destroy the structure of the file. Generally, do not modify the generated class file casually.
怪我咯2017-04-17 17:42:30
Although I have already answered it, I still want to say that the one that comes with windows will have gimmicks, which is annoying
天蓬老师2017-04-17 17:42:30
Use notepad++ to modify this one with hex editing mode. Constants in exe/dll are stored centrally. Many original game cheats directly modify the amount of money in the game and immediately enter the invincible mode
怪我咯2017-04-17 17:42:30
The questioner can learn about the class file structure. It can be modified with a suitable editor. The premise is to understand the scope and significance of the specific value of the changed position
PHPz2017-04-17 17:42:30
The format saved after vim modification is different from the previous .class file format. After all, it is a bytecode file.