简体   繁体   English

如何运行由用户创建的类文件,而不是由copiler创建的类文件

[英]how to run a class file created by user not created by copiler

I opened the class file in notepad. 我在记事本中打开了课程文件。 copied the content of it and pasted in another notepad and saved it at with the same class name with the ".class" extention. 复制它的内容并粘贴到另一个记事本中,并以相同的类名保存,扩展名为“ .class”。 then I tried to run that new class file and got the errors as. 然后我尝试运行该新的类文件,并得到错误。

Exception in thread "main" java.lang.UnsupportedClassVersionError: print : Unsupported major.minor version 8242.8224
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Class files are binary files, so you probably copied a corrupted view of it. 类文件是二进制文件,因此您可能已复制了损坏的视图。 You don't want to mess with Java class files by hand, unless you are willing to learn the JVM's bytecode. 您不想手工弄乱Java类文件,除非您愿意学习JVM的字节码。

If you REALLY want to do this, I suggest getting a hex editor that lets you copy and paste. 如果您确实要执行此操作,建议您使用一个十六进制编辑器进行复制和粘贴。

EDIT 编辑

One other thing, there's no value in messing with Java bytecode unless you are writing a compiler or something like Hibernate that really gets dirty with a class file at runtime. 另一件事,弄乱Java字节码没有任何价值,除非您正在编写编译器或类似Hibernate之类的东西,而它们在运行时确实弄脏了类文件。 Meaning there is literally no normal business case for manipulating bytecode and you are almost invariably going to introduce a series of "WTF WERE YOU THINKING?!" 这意味着实际上没有处理字节码的正常业务案例,并且您几乎总是会引入一系列“ WTF您是否在想?”。 level errors into a library or application if you don't do it right and with a good reason. 如果您做错了并且有充分的理由,则将错误归类到库或应用程序中。 One particular reason is that Java and JVM bytecode have a relationship much like C/C++ and Assembly. 一个特殊的原因是Java和JVM字节码之间的关系非常类似于C / C ++和Assembly。 Sure you can disassessmble a C/C++ app and throw in some hand-coded assembly. 当然,您可以取消对C / C ++应用程序的评估,并放入一些手工编码的程序集。 However chances are you will introduce undebuggable errors into the app (those errors will be related to your hand-jamming bytecode/assembly into the app, not the code from your codebase that you wrote in the high level language). 但是,您很可能会在应用程序中引入不可容忍的错误(这些错误与您对应用程序的人为干扰的字节码/程序集相关,而不是您用高级语言编写的代码库中的代码)。

This means your javac version is different from your java version. 这意味着您的javac版本与Java版本不同。 see 看到

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version 如何修复java.lang.UnsupportedClassVersionError:不支持的major.minor版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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