I have a jar that I want to take out a class file and add a few lines of code to it. I got class editor, but you can't actually change any code, you can change constants and that is all. I have a program that you can read the source code from a .class but you can't change anything on it. Is there a program or eclipse plugin that you can read and EDIT the source code from a .class file?
Have a look at decompilers. They'll transform the java-bytecode back to source code. One example of such a compiler would be JD: http://jd.benow.ca/
After editing the source code you would have to compile the code again and pack it in the respective .jar file.
If your file is an old enough version, you can use jasper to disassemble it into bytecode, edit the bytecode and reassemble it using jasmin. Unfortunately these tools have not been updated in some time.
They do not produce Java code; you'll have to learn Java bytecode. But it is more reliable than the so-called "decompiler" methods.
As Java .class files are in byte-code format, you cannot modify them the way you would edit a .java source file. Bytecode is a low level language closer to the machine language rather than Java itself.
If you need to modify the source, one option is to use a decompiler, e.g. JAD (Java Decompiler), to get a source file and then change it and recompile to .class using javac. Make sure you figure out which version of Java language (1.4, 5, 6, 7, 8) has been used for the original jar file.
What you need for this is a java de-compiler. This will take the bytecode out of your class file and convert it back to its source. From there you will need to recompile the .java files that the de-compiler produces.
Here are some java de-compilers that I have seen:
http://dcompiler.sourceforge.net/
http://jd.benow.ca
Related
Look at this question. When you open .class file with scala plugin enabled (Intellij Idea) it shows you scala code, bu when it is turned off java decompile plugin shows you a decompiled java code. Note that .class files which are compiled by javac decompiles even when scala plugin is enabled. That means that scala plugin "look at" some marker inside class files and intercept file content showing.
What the marker it actually uses? Is there a way to open .class file and change compiler (and/or) other infomartion to make those classes looks like they are compied by javac?
Each class file can have a SourceFile attribute which contains the name of the source code file. Since this is an arbitrary string, it’s a bit about conventions, e.g. for Java source code, it usually contains the file name only, without any package specific directories.
So there still is bit of interpretation of the information, e.g. if the specified name ends with .java, an IDE has to look up the known source tree for a matching file in sub directories matching the actual package.
Determining that the source file is not Java is as simple as recognizing that it has a different file name ending, then, whatever convention is used for the particular language may be used, if a plugin knowing it has been installed. Otherwise, most IDEs will simply look for any text file of that name and display it. There might be LineNumberTable attributes, telling how bytecode instructions map to source line numbers, allowing debuggers to step through the code even without understanding the source code syntax. I already stepped through code compiled from an XSLT file that way.
Of course, the pattern of the specified source code file name may also be used to decide which decompiler to use when the source file has not been found.
Intellij Idea
Intellij IDEA (assuming your question is about this IDE), just figures out from which source file a binary was compiled, and then displays the corresponding source.
It does not extract or produce the source code from the binary. It is just able to find the source for a given binary. You can do this by matching file names and paths. Intellij does probably a bit more, though.
In general
In general, there is almost certainly no good decompiler that can produce Scala sources from class files. Also the source code is not embedded, so all you can achieve is trying to match source code files with binary files.
The Java code you get from decompilation is what you get shown when the IDE could not find the corresponding source file.
Was given a couple of .class files but the .java files weren't sent with and I was hoping to find a way to get the .java files using the .class files. Thanks
You can use a decompiler to do so. One of the most major ones is JD-GUI.
JD-Core is a library that reconstructs Java source code from one or more “.class” files. JD-Core may be used to recover lost source code and explore the source of Java runtime libraries. New features of Java 5, such as annotations, generics or type “enum”, are supported. JD-GUI and JD-Eclipse include JD-Core library.
EDIT (2018-02-23): It seems that JD-GUI is incapable of decompiling bytecode compatible with Java 8+ JREs. This, obviously, changes the utility of my answer.
EDIT (2018-05-24): For replacing JD-GUI, I would recommend Luyten, which can be found here. It's very similar to JD-GUI, but supports Java 8 byte code, itself being based on Procyon.
You can use any of the java decompiler utility for this. There are a couple of few good utilities available over the internet, e.g., JD decompiler, you can also look for the eclipse plugin as well for the same.
To view java content from .class files many decompilers are available.
I'm using JD compiler which is very good.
http://jd.benow.ca/
If you want it for edit/ update puropse, one way is copy + paste from decompilers. Other solutions i'm not aware of.
You can use Jadclipse which is basically a Java Decompiler. It can be used with eclipse integration..
You can use decompiler to get .java files from a jar file or a .class file
I have a .jar file that represents a plugin that I am trying to mess with. This is an older version of the plugin, and a newer version was written by somebody else. I have this newer version as a project.
The newer project is full of .java files, and the old plugin is full of .class files. I can import the jar as a project, but it's still all class files. The differences between the class files and the java files are not particularly large, and I would like to see the differences between them. When I do this now, however, the text comparison changes the .class file from its normal representation in the editor to a binary representation. I know that if they were the same type of file, I could select the two and hit "Compare With". How can I do this between a .class and a .java file, or how can I turn one into the other in a way that still allows me to compare the two?
What would really be best is if there were some way for me to edit the jar, by turning the .class files into .java files.
It seems like what you will need is a decompiler to convert the Java .class files (bytecode) back into their original .java source files (text). Then you could compare to the two text files. This seems like it might be useful: http://java.decompiler.free.fr
You can use SOOT (http://www.sable.mcgill.ca/soot/) to do this. Two approaches are possible:
Decompile the .class files into .java files using Dava in SOOT, and then compare the .java files.
Convert both .class and .java files into an intermediate representation called Jimple in SOOT, and compare the Jimple files.
I think the second approach is more reasonable, because:
In the first approach, some Java files are manually developed, while the others are machine generated. Doing a diff on them creates results that are difficult to read.
The Jimple representation is very close to Java source code and relatively easy to read. Reading a diff result on this unified, machine generated format is much easier. Also, if you want, you can convert all Jimple files back to Java source code (well, this is sort of the third approach...).
Because it was a plugin, I was able to import it as a plug-in project, and there was a box to include the source folder. When I checked that I got access to the .java code and was able to diff successfully.
Is this possible to convert a .class file (from .jar external library) to a .java file? I'm trying to figure out whether it is possible or not because the source of the external library is unavailable.
What are the steps I need to take to do this?
use a java decompiler like "Cavaj". It will open the class into a txt format, copy the code to a file and save as .java
Use jad. Download it from here. It works fine with classes compiled up to SDK 1.4... 1.5, if I recall correctly.
The javap command takes class-names without the .class extension. Try
javap -c ClassName
javap will however not give you the implementations of the methods in java-syntax. It will at most give it to you in JVM bytecode format.
To actually decompile (i.e., do the reverse of javac) you will have to use proper decompiler.
http://download.cnet.com/Cavaj-Java-Decompiler/3000-2213_4-10071619.html
or may be this be of some help Java Decomilers
it is Possible to convert a class file to java file without using any tools . e.g Decompiler or something else ?
Can I change a java file, when I'm running it on JVM, using this file?
For example:
I run abc.java. In this program I've a textarea and I want to paste text from the text area into abc.java and save the changes. Is it possible?
3 steps:
modify your .java file just like you would modify a text file.
compile that .java file from within your already running JVM using javax.tools compilation tools
Instantiate your freshly compiled class using the ClassLoader.
No. Maybe what you want is dynamic define classes at runtime. If so, you could choose dynamic language like Groovy.
The JVM runs class files (.class) and not Java (.java) files. To "convert" a *.java file to a *.class file it needs to be compiled firs.
So change a *.java File will not infuent the JVM in any way, because it is compleatly not interessed in.
Only because of a totality answer
You can write a programm (.java) that when it is compiled an run (.class) change its own source file (.java) (for example changing a string by user input) compiles it (.class), and restart it selfe with its new compiled form. But this is defently not what you want! One would write this kind of software only to prove that this insane idea would work.
Anyway, what you need is a way to store data, in a file or a database. (Have a look at the java.io package for file handling, and for an tutorial).
(new answer for updated question)
"In this program I've a textarea and I want to paste text from the text area into abc.java and save the changes."
This seems like a strange loop, self-modifying java code?
Then yes, the tools nrobey describes would be the way to go, though I agree with Ralph that it might be a bad idea.