Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Read/Write class file

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Read/Write class file

    Please I'm trying to extract strings from a class file, modify and write to the file, but I don't know how to do it. Since class files are not text files, I can't use the normal way of reading and writing.
    I would appreciate if someone could help by giving me a Java code to achieve this.
    Please help me...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read/Write class file

    There is documentation for the contents of class files. I don't remember where it is stored. An internet search should find it.
    This may help: http://docs.oracle.com/javase/specs/...sFile.doc.html

    Once you get the layout of the bytes for the class file, you will be able to read the bytes in the file and find the values of the Strings used.

    Given the format definition you may be able to write out the updated class file,
    but it will be lots of work to create the code.

    There are java decompilers.

    What are you trying to do? Is there a better way?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Anaphase21 (December 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read/Write class file

    Thanks for response @Norm.
    Actually I'm building a J2ME application that will translate the class strings from one language to the other. I'm done with the translation code and now needs a way of obtaining the strings, translating, then writing it back. From oracle's documentation of the VM specification, I learnt that strings are stored in the Constant pool as utf8 encoding. I also learnt the value for the string constant as 8. I don't have much knowledge about the class file format. Atleast, I would appreciate if the algorithm for doing this task is shown to me.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read/Write class file

    Algorithm? Read the specifications, look at some class files in a hex editor to see what they look like and write some code that reads the bytes and finds the desired parts of the file.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Read/Write class file

    Ok! I will try that. When I get hooked up, I will come back.

Similar Threads

  1. write & read/load to/from file
    By klskl in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 26th, 2013, 10:39 PM
  2. Read and write to a text file and calculate values
    By s.sariyan in forum What's Wrong With My Code?
    Replies: 19
    Last Post: November 28th, 2012, 10:19 AM
  3. help with GUI .. read file and write file
    By kernal in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 14th, 2012, 10:38 AM
  4. Read and write file in Java
    By waiheng1986 in forum File Input/Output Tutorials
    Replies: 1
    Last Post: March 18th, 2012, 11:54 AM
  5. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM