-1

I have a requirement in which I need to read java class file as a string in java.

my code is

String path = "C:/java/MainClass.class";
String content = new String(Files.readAllBytes(Paths.get(path)));

When ever I print content variable, I am expecting the code to work as it works for simple text file, my expected output is something like this

public class MainClass { public static void main(String[] args) { System.out.print("This is a test file"); } }

In future I also want to read files in other formats as a string for ex: .cs, .py, .config

Please Let me know what are the changes I need to take care so that I read it as a simple text file.

Cœur
  • 37,241
  • 25
  • 195
  • 267
vjnan369
  • 833
  • 16
  • 42
  • 1
    Java classes compile to byteCode - bytes are not necessarily convertable to Strings – Scary Wombat Oct 26 '16 at 07:06
  • 1
    `.class` files are compiled code. You might want to change to read `.java` files instead – kjsebastian Oct 26 '16 at 07:06
  • Your expectation is wrong. The ".class" file does not contain the java source code. You can see that opening one with a simple text editor. Outside that, you should be able to read it in as string like any other plain text file. – Fildor Oct 26 '16 at 07:06
  • open a `.class` file with notepad and see what you get. It won't be what you expect – jhamon Oct 26 '16 at 07:06
  • The requirement is that you spent more time studying your requirements: like doing some research to **understand** them in the first place. So that you dont need other people to point the obvious to you. – GhostCat Oct 26 '16 at 07:09

2 Answers2

4

Please Let me know what are the changes I need to take care so that I read it as a simple text file.

What you are trying to do is not possible. The ".class" file contains bytecodes not source code, and bytecodes cannot be turned back into the original Java source code.

The best you can do is use a decompiler. But beware:

  • a decompiler won't give you the original source code back, and
  • in many case a decompiler will give you stuff that is unreadable or uncompilable Java code.

Another alternative would be to use a disassembler, to get the code in format like the output of the javap command.


On the other hand, if you simply want to read a ".java" file in Java, your code will do that just fine. The only wrinkle might be the character encoding used in the source code file, but most Java source code is encoded in ASCII ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Apart form his wrong assumption about the content of a .class file, the part of the question concerning reading file contents as string is still possible. (I did **not** downvote) – Fildor Oct 26 '16 at 07:08
  • I disagree. He asked if the .class file can be read as "a simple text file". The implication is that it would be human readable. No matter what character encoding you chose, a class file cannot be decoded as simple text. – Stephen C Oct 26 '16 at 07:13
  • Thank you @StephenC, Fildor – vjnan369 Oct 26 '16 at 07:15
  • I agree with that. I was disregarding the "human readable" part. I am also assuming he is confusing .class and .java files since he goes on wanting to read in .cs, .py, .config etc ... – Fildor Oct 26 '16 at 07:15
  • Probably possible using some decompiler http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files after the reading. Not 100% good but suitable for simpler cases – Akceptor Oct 26 '16 at 07:22
0

The cfr library supports reading 'class' files into strings at runtime

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '22 at 18:25
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32758093) – user16217248 Sep 26 '22 at 02:24