eclipse编译java项目class文件_用Java实现JVM第二章《搜索class文件》

9ac2851de8c32f82f040475517e85262.png

搜索class文件

案例简述

本章节主要了解Java虚拟机从哪里寻找class文件并且读取class内字节码

环境准备

1、jdk 1.8.0

2、IntelliJ IDEA Community Edition 2018.3.1 x64

3、Notepad++ (插件安装HEX-Editor,用于查看class字节)

配置信息

1、调试配置

2.1、配置位置:Run/Debug Configurations -> program arguments

2.2、配置内容:-Xjre "C:Program FilesJavajdk1.8.0_161jre" E:itstackgitistack-demoitstack-demo-jvmitstack-demo-jvm-02argetest-classesorgitstackdemoestHelloWorld

代码示例

itstack-demo-jvm-02

├── pom.xml

└── src

└── main

│ └── java

│ └── org.itstack.demo.jvm

│ ├── classpath

│ │ ├── impl

│ │ │ ├── CompositeEntry.java

│ │ │ ├── DirEntry.java

│ │ │ ├── WildcardEntry.java

│ │ │ └── ZipEntry.java

│ │ ├── Classpath.java

│ │ └── Entry.java

│ ├── Cmd.java

│ └── Main.java

└── test

└── java

└── org.itstack.demo.test

└── HelloWorld.java

pom.xml

com.beust jcommander 1.72

CompositeEntry.java

package org.itstack.demo.jvm.classpath.impl;import org.itstack.demo.jvm.classpath.Entry;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;/** * http://www.itstack.org * create by fuzhengwei on 2019/4/24 */public class CompositeEntry implements Entry { private final List entryList = new ArrayList<>(); public CompositeEntry(String pathList) { String[] paths = pathList.split(File.pathSeparator); for (String path : paths) { entryList.add(Entry.create(path)); } } @Override public byte[] readClass(String className) throws IOException { for (Entry entry : entryList) { try { return entry.readClass(className); } catch (Exception ignored) { //ignored } } throw new IOException("class not found " + className); } @Override public String toString() { String[] strs = new String[entryList.size()]; for (int i = 0; i < entryList.size(); i++) { strs[i] = entryList.get(i).toString(); } return String.join(File.pathSeparator, strs); } }

DirEntry.java

package org.itstack.demo.jvm.classpath.impl;import org.itstack.demo.jvm.classpath.Entry;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;/** * http://www.itstack.org * create by fuzhengwei on 2019/4/24 * 目录形式的类路径 */public class DirEntry implements Entry { private Path absolutePath; public DirEntry(String path){ //获取绝对路径 this.absolutePath = Paths.get(path).toAbsolutePath(); } @Override public byte[] readClass(String className) throws IOException { return Files.readAllBytes(absolutePath.resolve(className)); } @Override public String toString() { return this.absolutePath.toString(); }}

WildcardEntry.java

package org.itstack.demo.jvm.classpath.impl;import org.itstack.demo.jvm.classpath.Entry;import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.stream.Collectors;/** * http://www.itstack.org * create by fuzhengwei on 2019/4/24 * 通配符类路径,继承CompositeEntry */public class WildcardEntry extends CompositeEntry { public WildcardEntry(String path) { super(toPathList(path)); } private static String toPathList(String wildcardPath) { String baseDir = wildcardPath.replace("*
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
查看javaclass文件,在网络上搜索了一下资料,这样就很方便的去查看别人的代码 在myeclipse中查看class文件,就像查看普通的java文件一样,通过以下步骤可以做到: 安装设置步骤:1. 下载jadnt158.zip 。 2. 下载net.sf.jadclipse_3.3.jar(对应eclipse3.3) ,二者都可以去CSDN上下载。 3. 将jadnt158.zip 解压,拷贝jad.exe 到%JAVA_HOME%\bin\ 目录下。 4. 安装插件net.sf.jadclipse_3.3.jar (将此文件拷到eclipse 的plugins目录(%eclipse_home%\plugins)下,例如C:\MyEclipse Enterprise Workbench 6.5GA\eclipse\plugins下)。 5. 设定路径:进入Window -> Preferences -> Java -> JadClipse,Path to decomiler设置为jad.exe的绝对路径,例如C:\j2sdk1.4.2\bin\jad.exe,Directory for temporary files设置为存放临时文件的决对路径这里我把它设置为C:\Documents and Settins\桌面\.net.sf.jadclipse 。 6. 在Eclipse 的Windows—> Perferences—>General->Editors->File Associations 中修改“*.class”默认关联的编辑器为“JadClipse Class File Viewer”。如果没有*.class,则点击“add"增加,如果没有jadclipse Class File Viewer,可以点击“Add-Internal Editors”,查找是否有jadclipse,若还是没有,则说明net.sf.jadclipse没有复制到正确的位置. 安装设置完毕,可以双击class文件,测试一下,正确的情况是:反编译成功,可以显示class文件(无需增加src源文件)为普通的java源代码样式,另外,在Myeclipse菜单栏上多出了一个JadClipse菜单. 现在可以通过myeclipse直接查看class文件,阅读代码效率高了许多~~~~~~ 安装时应注意:jadclipse的版本一定要和eclipse的版本一致!比如,我用的Myeclipse版本为6.5,其包含的eclipse版本为3.3。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值