Is it possible to view dynamically generated bytecode in eclipse? - java

I know of several solutions for files that end in ".class", where you can use the ByteCode Outline plugin, or the Bytecode Visualiser plugin, or even the built-in "javap-like" viewer for classfiles.
But I would like this ability for runtime-generated code while debugging! What I am trying to achieve is to see the actual generated code from a clojure call executing. This uses ASM to generate classes on the fly.

Using the eclipse IDE, you can combine JAD(a java decompiler) with the JADClipse plugin to decompile your class files on the fly whenever the debugger attempts to open a class file that has no linked source. Without eclipse, you can use JAD as an independant executable, but it is less user friendly.
JAD will generate the java source for a class file even if it was built with a java assembler. This is possible because the java language is close enough to the functionality of the jvm. The only class files this wont work on is ones that have been run through an obfuscator to prevent decompilation

Apart from JAD, there are other tools. One's called JD (Java Decompiler). It also has an eclipse plugin and is a little better than JAD since it supports Java 5, JAD only supports Java 4 (as far as I know there hasn't been any development on JAD since 2001 or something). A tool that seems to be supporting Java 6 is DJ Java Decompiler, but I haven't tested that yet.
And I don't know if any of these tools actually support on-the-fly-generated code, on the other hand I don't really see why they should not.

Related

Using Java Decompiler (JD-GUI) on a .class file gives me multiple errors on src file, but it runs fine within a .jar? How does it work?

I'm new to Java and I'm trying to understand something. I managed to get a .jar file for a local device to communicate with my laptop. It works fine, but I would like to do an upgrade and scoop around the codes and change some details. I used a Java Decompiler on the .jar file and managed to take a look at the class files. However, whenever I decompile them into source codes, errors will occur in my IDE, and I'm unable to build them. What exactly happened? If it's working fine in the .jar why does it produce errors?
Compilation is a lossy process, so decompilation is always a "best effort" endeavor. Java bytecode is unusually high level, so Java decompilation is relatively easy compared to other languages, but it will still never be 100% complete. Therefore, you should not be surprised to see errors when re-compiling decompiled source.
If you want to work directly with bytecode, I would recommend using the Krakatau disassembler and assembler. Instead of decompiling to Java source, the disassembler converts bytecode into a human readable assembly format which is specifically designed to be converted to and from bytecode losslessly. However, it requires a good understanding of low level Java bytecode to use.
Completely agree with Antimony. There is no decompiler which decompiles always without any error. Sometimes it decompiles well, sometimes not.
The best way is to have several decompilers and use them each by other to decrease errors in decompiled source.
If you use Eclipse I would suggest https://marketplace.eclipse.org/content/enhanced-class-decompiler
If you use IntelliJ try this decompiler Jar Explorer.

See what's inside classes we import from jar files

Started coding in java , just wanted to see the implementation of classes and interfaces we actually import to use their methods.
Ex:
import java.util.Scanner;
I am curious to see what's inside the class Scanner(){}
Is that possible?
Tried Searching but couldn't find the implementation and definitely did not understand how they hide them from public.
Please follow the following Steps to See Java predefined classes implementation
1) Get rt.jar (It contains all Core Java API Classes which required for JRE at Runtime)
Download it from the internet directly or Search it in JDK at Path like C:\Program Files\Java\jdk1.8.0_181\jre\lib and copy it to other location on your Desktop
2) Extract the copy of rt.jar
Extracting .jar file with command line
https://www.wikihow.com/Extract-a-JAR-File
After extracting you can see all the Core Java Classes in .class file (cant read it Directly) which we use normally in our programs
3) Download tools like cavaj java decompiler to open and read .class file
You Can see any code in Any jar with this approach... Its very smart way to understand basics of java with fun ..
There is nothing "hidden from public" concerning java.util.Scanner.
#shmosel already linked to the source of it, #Stultuske pointed out, that there are various websites covering source code of java.
Modern IDEs also allow you to view into source for your libraries (aka .jar files).
My Netbeans IDE shows source from known jar files whenever I ctrl-click on a class.
Same goes with Eclipse, IntelliJ, ...
For a lot of open-source libraries (e.g. from Apache) there is also source code available. Just by cloning the version control repository, downloading source packages (zip, ...-source.jar) or browsing the version control repository online.
If project is buildable with maven, the IDE can even download and show sources for open source project automatically.

How to create a processing library from pde files

I have this nice set of processing .pde files that together would make a good library. I don't want to use Eclipse or some other GUI editor. I'm ok with Ant if need be.
To create a processing library, I need to put a jar file in the processing libraries folder (with some special naming conventions). I can create a .java files from the .pde files by exporting the application. If I modify these a bit, I can compile them using javac, but it needs to import processing.core.*;. I point it at my own processing core.jar, but at this point I get a
class file has wrong version
It seems processings core.jar was created for a different version of javac then I'm currently running.
So here's the question: am I totally off track and is there a better way to do this, or should I continue and download the Processing source code and compile it myself, in order to be compatible with my javac ?
I highly recommend using Eclipse or Intellij rather than the Processing editor. The Processing editor is great for writing sketches, but if you're building a library, you should probably use a more advanced IDE.
But it sounds like you're using an old version of Java and should upgrade. I'm pretty sure Processing is compatible with Java 8, which means you're using Java 7 or older. Java 7 has been end of life for quite some time now, and you should upgrade anyway.
To find out which Java you're using, try this command:
javac -version

Conversion of class file to Java files

I have one War files (a Java application) consist of 64 class file along with image/CSS/JS files. I have lost the source of this war file. Now I want to do some changes in code. I am looking for some expert advice from community on following questions.
Is there best way to convert .class files to .java file without losing any section of code?
What are the tools if any for this task?
What are challenges/drawbacks of converting .class files to .java files to recreate war file?
If you do not want to spend horrible times in understanding decompiled bytecode, please start using a version control system and a remote repository for any project of more than two classes.
In particular, you can use Git with GitHub, open source are hosted for free while if you want to protect your code you can get a commercial subscription which is cheap anyway.
For this time, help yourself with a java decompiler such as JD:
Use a java decompiler to decompile the bytecode.
Create a git repository out of the decompiled java classes
Constantly synchronize it with the remote repository
Conversion to Java Bytecode is almost completely reversible. As #StephenC said, In the regenerated sources, you will lose comments, names of local variables etc. It might not compile right away but you should be able to get it to compile again, with a little modification.
You need a Java Decompiler to do that. Try JD-GUI or DJ Java Decompiler
In case you obfuscated your sources, the sources you recover will not be harder to understand and reverse engineer. Otherwise, with just a little tinkering, you should be able to get back your sources.

Reflector for Java?

Is there a Java equivalent to .NET Reflector?
Edit: more specifically, decompiling is what I'm after.
See How do I decompile Java class files? There's a link there to JD-GUI, which seems to be about what you're looking for.
Edit: Also see Open Java *.Class Files.
Edit 2: And Best free Java .class viewer?, which specifically mentions .NET Reflector.
From what little I know the functionality of .NET Reflector is available in pretty much all Java IDEs, including Eclipse. Just add a jar file to a projects and you can browse its classes just as you browse your own classes.
For the decompiler aspect (as opposed to the pure class browser) there are some alternatives as well. The JDK tool javap only decompiles to byte-code so it isn't really useful to get to the source code (but might help with getting an understanding of the code).
JAD is a pretty popular decompiler in the Java world and it produces compilable Java code most of the time (some bytecode sequences aren't easily translatable to valid Java 'though, so some corner cases exist).
You can get JAD for decompiling .class into .java. It's pretty good at it.
If you use Eclipse get the jadclipse plugin too. You'll need to configure the path to the jad.exe. Next time you open a .class you'll see the decompiled source instead of the .class declaration. And you can browse with F3 too. It's very useful.
I'm not super familiar with Java, but does JBrowser work for you?

Categories