Write comments on class file: Java - java

Is it possible to manually write comments in a class file or an API that can, if so, how?

A java class file is a file that contains the compiled java bytecode. So there is no practical way to manually edit this file. Comments are usually put into source code to communicate to developers what a certain section of code is for and/or what it does and why. As a person cannot (practically) directly edit a class file or read it there isn't any reason to add comments to it. When a source file is compiled into bytecode from Java the comments are not compiled into the bytecode. So even if you could manually edit the file there is no way to add comments to it.

A .class file is a .java file after it has been compiled.
When a file is compiled, only functional code gets built, comments are stripped.
There is no syntax that will allow you to have comments in a .class file that will still be functional afterwards.

There's no standard way to put comments in a classfile, because it's not something people normally need to do.
However, if you just want to stick textual metadata inside a classfile for some reason, there's plenty of places to put it. The JVM specification defines numerous places where you can add custom metadata. You can even make it visible to code at runtime by creating a runtime annotation.

As mostly everybody has said, a .class file is a compiled .java file, and only the JVM can read that. But you could put a // at the beginning of any line to create a comment in the .java part of it. I don't even think it's possible to make a comment in a .class because of the fact that its mostly UTF-8 characters (which I really don't get).
Here's an example:
class YourClass{
public static void main(String[] args){
//comment line
System.out.println("normal line");
}
}
That's at least how i do stuff like this.

You should not write anything in a .class file. The class file is written by the javac command and will not include any of the comments you have made in your .java file. The .class file is for the eyes of the JVM only, and the JVM doesn't care about comments.

As my understanding, you are asking about the comments in Java.
There are three kinds of comments in Java:
/* text */: comment detail in the "text" and compiler does not compile everything from /* to */.
/** documentation */: A kind of documentation comment(doc comment, explain a complex business in code). The compiler ignores this as the text comment form /* and */. More detail you can search in Java doc.
// text
The compiler ignores everything from // to the end of the line.

Related

ASM4/5: How can I embedded source code in ASM4/5

I am about to transform(manipulate) a lot of classes and to allow easy debugging and transparent communication of the changes applied to the code, I want to add java source code equivalent of the manipulated class.
In order to add java code I would be able to use existing source code and manipulate it in parallel. Then I need to store it along with the class.
Visiting the class file format for JDK 8, I noticed that no attribute exists to directly embed source code. Remembering the old times it was possible to include the source code within a class file. What do I miss? (I only found the attribute for specifying a source file). Also the option in the compiler tab of Eclipse does only show options to embed file names... .
Beside seaming to store source files separately, I wonder if it is feasible to reverse engineer (decompile) the class file. If one provide information about local names and parameter names, this might even be a better option.
The ASM documentation stated out that a tool is available to even decompile byte code.
Does anyone has some insights or experiences to share on this matter?

Opening and editing java classes in not java bytecode view

How to open and edit java class files? I've searched, but I either found how to open them without being able to edit them, or how to be able to edit only the bytecode. I want to be able to read and edit .class in the "normal" view. (as source code, not bytecode)
What you are asking for is basically impossible. The ".class" files do not contain source code, and do not contain enough information to reconstruct the original source code.
If you are a bit lucky, a good decompiler would be able to create compilable source code that means the same thing as the ".class" files. However:
That decompiled source code won't have the original comments.
The original names of any local variables are not recoverable.
The structure of the decompiled code may be different; e.g. string concatenation, for loops and try/catch structures may be transformed.
The code is not guaranteed to be correct, or compilable at all. (It depends on the decompiler, and how well it deals with the version of Java you are trying to decompile.)
And if the code you are trying to edit was obfuscated, then your chances of success are greatly reduced. An obfuscator deliberately transforms the ".class" files to remove useful information, and to confuse decompilers.
To my knowledge, no IDE supports editing of ".class" files like this.
Before my suggestion, I wanna point out that as far as I've searched, there has yet to exist a free decompiler that allows you to edit the source code produced, and save it automatically. I believe this is due to decompilers only being able to attempt to decompile the code, and the source code produced is not always exact/error free/compilable.
What you can do
You must use a decompiler, such as JAD, copy the source code that it produces and paste it into a new file.
As for the download link, you can find that on google, as I am unsure of the safest place to get it.
A decompiler does its best at converting the content (bytecode) within .class files into readable Java source code. Not only will some identifiers (method, variable and class names) will be replaced with generic names, meaning String name; in bytecode might decompile to String aString1;. Random variables might also be generated (tmp variables), which can lead to the produced code being unable to compile.

Hide a class in a .jar

Whenever I build my app all classes (logically) are visible in the .jar that comes out of it.
Aswell as a class that holds information to my MYSQL server (for the app to connect to). But I dont want this information to be publicly visible!
How can I "hide" this code or "hide" the class?
Thanks!!
I think you mean you dont want someone to do reverse engineering with your .class inside your jar file. There are many decompilers that can do that.
So you would need to Obfuscate your code with an obfuscator utility.
The process of obfuscation will convert bytecode into a logical
equivalent version that is extremely difficult for decompilers to pick
apart. Keep in mind that the decompilation process is extremely
complicated and cannot be easily 'tweaked' to bypassed obfuscated
code. Essentially the process is as follows:
Compile Java source code using a regular compiler (ie. JDK)
Run the obfuscator, passing in the compiled class file as a
parameter. The result will be a different output file (perhaps with a
different extension).
This file, when renamed as a .class file, will be functionally
equivalent to the original bytecode. It will not affect performance
because a virtual machine will still be able to interpret it.
Here is an article describing this process in more detail and
introducing an early obfuscator, Crema:
http://www.javaworld.com/javaworld/javatips/jw-javatip22.html

How to de-compile a .class file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I “decompile” Java class files?
I m new to java and i was wondering if there is a way to retrieve the source code of a file (i.e. the .java file) from its compiled file (.class file).
I have read about the compilation process i.e. about the parsing, the syntax and semantics trees, the intermediate code generation, macros used etc. It sounded like a very complex process. Reverse engineering it will be even more difficult. Therefore, I think it is not possible but i had to ask. I googled it but couldn't find anything satisfactory.
Also i learnt about code-obfuscation in my class. If de-compilation is possible how will it behave for an obfuscated file?
Thanks in advance.
[edit : I won't complete the answer as this question is a duplicate, this is just an additionnal comment on the second part of the question]
When you obfuscate a class, you remove everything that isn't needed for the execution but is present in the file, like the names of the method.
In fact, obfuscating an isolated class doesn't remove a lot because other classes need the names of the methods or visible fields. So usually you obfuscate a group of class togethers using a tool like proguard.
Yes there is, very simple by the way, get a program called DJ Java Decompiler (search in google it's easy to find) and after you install it, you just have to open the .class file, and it shows the code all formatted and indented.
Microsoft Windows only application

Is it possible to edit a pre-existing .class file from within my program?

This may seem like an odd thing to ask, but it'd take me forever to explain why I need it...
What I need is a way to edit a pre-existing Java .class file within its JAR file, with either a command prompt, or within my Python program. I need it to happen automatically, once the user pushes a button.
I have absolutely no clue how to do this, or if it's possible.
A jar file is a zip package, you need only to extract the file, edit the content and put it back. The harder part is how to edit the .class file. The java .class file is a binary format , there're several libraries may help you.
Yes you can do this. Now how you gonna do it depends upon what you want to do. For your cross-cutting issues look at AspectJ. Using AspectJ you can add your custom code even after the class is compiled.
You have a problem with this approach, if the class has already been loaded by a JVM classloader, as it may not actually reread the .class file again until the application has been rerun.
I know that there exists the BCEL but I've not used it, so I dont know if it can be used a) from python, or b) during runtime.
EDIT: Actually, Jeffrey's list is better as it provides a much more comprehensive list of Byte Code manipulators.

Categories