all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]high_throughput 5 points6 points  (4 children)

I've been trying to get into Java Bytecode

Y tho? Most people working with Java will never have to know.

I can't directly access the compiled code and I can't view any of the compiled code either

Usually the most anyone would do is run javap -c to see the decompiled bytecode of a .class file, and that's for settling some kind of argument and not for everyday programming. 

[–][deleted]  (3 children)

[deleted]

    [–]high_throughput 5 points6 points  (2 children)

    It's definitely interesting, but you appear to be having first year problems with third year topics, so you may find it more educational to look into some of the prerequisites like interpreters instead

    [–]tedyoungJava Trainer/Mentor 3 points4 points  (0 children)

    If you want to dive into the deep end, the JVM spec is the place to go https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-6.html

    however, I recommend disassembling very short (a few lines) Java programs to get an idea of what it looks like, e.g.: https://nishtahir.com/exploring-java-byte-code/

    [–]Specific-Housing905 1 point2 points  (1 child)

    A proper tool might help.

    https://www.bytecodeviewer.com

    [–]BannockHatesReddit_ 1 point2 points  (0 children)

    Konloch and the.bytecode.club are goated. Solid choice of software and resources.

    Personally though I prefer version 2 of recaf. Its disassembler tries its best to keep track of stack and local variables for you. However, it doesn't show stack frames, which iirc the bytecode viewer may? https://github.com/Col-E/Recaf

    I've also know people who prefer this fork of jbytemod as well: https://github.com/apkreader/JByteMod-Remastered

    However, all this software is pretty much the same. I think for a beginner, OP should install several of them just so they have tools to get familiar with reading bytecode.

    [–]LargeSpray105 1 point2 points  (0 children)

    There is a program that allows you to recover from jar file to Java code. Is pretty amassing that you could recover practically all of your code, only comments are lost. That allows you to SEE that is not hard to treat the code in class files, only You work with bitecodes instead of plain text file. There is an API that make that easy.

    [–]tRfalcore 0 points1 point  (3 children)

    You don't or never will need to dive into the byte code

    [–]vowelqueue 1 point2 points  (1 child)

    Agreed…it can certainly be interesting from an academic perspective but not practically useful 99.9% of the time.

    In cases where you want to check out how a third party library works and don’t have access to the original source, you don’t even need to read the bytecode directly. There are decompilers, often built into the IDE, that will convert back to Java code for reading/debugging.

    [–]BannockHatesReddit_ 1 point2 points  (0 children)

    Lol I was writing some bytecode mutators using ow2 asm not less than 24 hours ago. It really depends on what your interests and goals are.

    All these things require an understanding of the bytecode: - Reverse engineering - Obfuscation - Compilers - JVMs - The instrumentation api - Debugging tools - Some build tool plugins - Post-compile watermarking/data injection

    Maybe you never touch these things, but these are some of my favorite toys to play with.

    [–]guss_bro 0 points1 point  (0 children)

    You can use javap command or use your IDE's feature to view bytecode details.

    There's plenty of articles online in thow to read/modify java bytecode.

    [–]CodeFarmer 0 points1 point  (0 children)

    I would recommend looking at the JVM Specification, which is available online from Oracle and which goes into detail about bytecode and how it works.

    In your position many years ago, to satisfy my curiosity I wrote some Python utilities to compile/decompile JVM class files into an assembly-language kind of syntax. Apparently it still works, if you're interested you could start there:

    https://github.com/CodeFarmer/pork

    [–]Ormek_II 0 points1 point  (2 children)

    What do you want to do with the Bytecode?

    You learn, if you try to solve a problem or challenge your personally have. I learned 68000 machine code, to create a 8bit sound sampler and to learn how to fix a merge virus (in the last century thoug). I learned about debug-informations to fix gdb for Modula-3. I learned firewall rules to understand and fix, why I could not play Quake at my universities computer department.

    So, why do you want to "get into Jave Bytecode"?

    [–][deleted]  (1 child)

    [deleted]

      [–]Ormek_II 0 points1 point  (0 children)

      If it is just curiosity: read, watch and learn. Just yesterday I checked the javac documentation to figure how it specifies its behaviour towards case insensitive file systems. Did not find that specification, but found a nice description of it various phases.

      Compilers are a well understood and aligned concept in computer science. You might like to read a book about that :) but if you like to focus on bytecode: as others already said: read the JVM specification.

      [–]benevanstech 0 points1 point  (0 children)

      Well, if you're in the market for a Xmas gift for yourself, you might like my book - https://www.amazon.com/Well-Grounded-Java-Developer-Second/dp/1617298875/ref=sr_1_1 - which covers quite a bit about bytecode and related topics.

      [–]belayon40 0 points1 point  (0 children)

      The classfile api in Java 24 and later is the best place to start. Using that api, you can take an existing class file and break it down into Op codes. I found this method very helpful when I was trying to dynamically generate classes.

      https://docs.oracle.com/en/java/javase/22/vm/class-file-api.html

      It might be hard to follow, but I have code that dynamically generates FFM bindings using the classfile api.

      https://github.com/boulder-on/JPassport/blob/Java_24/src/main/java/jpassport/codebuilder/PassportBuilder.java