如何在我自己的 JAVA GUI 中查看 jar 文件的源代码?
我有一个带有一个列表框的 GUI,用于显示类中的方法列表。 我可以使用反射来实现它。 但是我可以在选择方法名称时在另一个文本区域中查看源代码吗?
我知道反编译器。但我不想在他们的窗口中看到源代码。
我想使用一些第三方库,以便我可以在我自己的 GUI 中看到特定方法的源代码。
请建议是否有可用的 API。
I have one GUI with one list box to display the list of methods in the class.
I can achieve it using reflection.
But can I view the source code in another text area on selecting the method name?
I knew about decompilers. but I don't want to see source code in their window.
I want to use some thirdparty lib so that I can see the source code of specific method in my own GUI.
Please suggest if there is an API available for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将需要某种可以链接到的反编译器。我不确定是否有任何库,但这里有一个 JD Java Decompiler 的链接。
请记住,在编译过程中您会丢失变量名称等,因此如果反编译,生成的源代码可能会不太可读。
如果您有权访问源代码,则可以将其链接到类文件,并在链接的源文件中找到所选方法源。这可以通过对源文件进行简单的一次性解析来实现。
您最大的问题是确定方法何时结束,一个简单的解决方案是对
{
和}
进行计数,并确定{
何时结束> 方法声明已关闭。You will need a decompiler of some sort, that you can link to. I am not sure there are any libraries, but here's a link to the JD Java Decompiler.
Remember that you lose variable names and such during compilation, so if you decompile the resulting source code may be less readable.
If you have access to the source you could link it to the class files, and find the chosen method source in the source files linked. This can be achieved by a simple one-pass parse of the source files.
Your biggest problem will be determining when a method ends, and a simple solution is to count
{
's and}
's and determine when the{
of the method declaration is closed.这是一个老问题,但鉴于反编译器的情况在过去一年中发生了显着变化,我觉得值得复活。
Procyon 是一个开源框架,其中包含 Java 反编译器等。它是用 Java 编写的,并且反编译器 API 可以相当轻松地集成到另一个应用程序中。事实上,已经有两个第三方 GUI 前端,包括 SecureTeam Java Decompiler。
CFR 目前还没有可用的源代码,但它也是一个优秀的反编译器。它也是用 Java 编写的,虽然我还没有尝试将它与现有应用程序集成,但它肯定是可能的。
This is an old question, but seeing as the decompiler landscape has changed significantly in the past year, I feel it's worth resurrecting.
Procyon is an open source framework that contains, among other things, a Java decompiler. It is written in Java, and the decompiler APIs can be integrated into another application fairly easily. In fact, there are already two third-party GUI front-ends, including the SecureTeam Java Decompiler.
CFR does not have source code available yet, but it is also an excellent decompiler. It too is written in Java, and while I have not tried to integrate it with an existing application, it should certainly be possible.
我曾经创建过包含其自己的源代码查看器的应用程序。我认为它是反编译器的一个很好的替代品,因为反编译器可能具有相当大的依赖性。
我使用的是 NetBeans,因此打包
.java
文件就像更改一个过滤器选项一样简单。我检查了 java 属性以找到 jar 文件,并像扫描任何 zip 文件一样扫描 java 源文件。使用这种方法,拥有一个带有填充源文件的 JTreeTable 和显示源代码的 JTextArea 的 GUI 就很简单了。我相信您可以通过添加一个步骤来执行相同的操作 - 剪辑源以仅包含选定的方法。我认为它应该归结为简单的解析器,一个计算左括号和右括号的解析器。
I once created application that included it's own source code viewer. I think it's a good alternative to decompilers, which can come with quite dependencies.
I was using NetBeans so packaging the
.java
files was as easy as changing one filter option. I checked java properties to find the jar file and scanned it just as any zip file for java source files. With this approach having a GUI with JTreeTable populated with source files and JTextArea displaying source code was trivial.I believe You could do the same with addition of one step more - clipping the source to contain only the selected method. I think it should boil down to simple parser, one that counts opening and closing brackets.
我将保留之前的答案,以备您需要时使用,但 JODE 已经很长时间没有更新了。四处搜索,我找不到任何开源或以库形式提供的反编译器。
JODE 可能正是您想要的。核心反编译器作为 GNU LGPL 下的库发布,因此您可以毫无问题地将其集成到您的程序中。I'm leaving the earlier answer up in case you need it, but JODE hasn't been updated in a long time. Searching around, I can't find any decompiler that is open-source or available in library form.
JODE may be just what you want. The core decompiler is released as a library under the GNU LGPL, so you can integrate it into your program with no issues.