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 7 of 7

Thread: Calling method from .class file

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calling method from .class file

    Hello!

    I have a project I work on and I am interested if it's possible to call a method inside a .class file from another java file (without decompiling the .class file).

    Eg:

    Inside my.java I call method buildGraph() inside the Graph.class file.

    I don't think it's possible, but look at the project's specs:
    Graph.class: Working Graph class file. You can use this while you are working on Tasks I/II. My Graph class prints a lot of output to let you see what is happening.

    Thank you!
    Alex


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Calling method from .class file

    I think you need to understand how the compiler works, it creates .class files from source files .java, you actually cannot call a method from a .java file, but you can from the compiled file .class.
    So to answer your question, yes you can call a method from a class file, just make sure it resides on your classpath so the JVM you are running under can see it.

    Incidentally IDE's are effectively looking at the .class files to determine what methods are available, this is obviously hidden from the developer.

  3. #3
    Junior Member shadihrr's Avatar
    Join Date
    Jan 2010
    Location
    home
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Calling method from .class file

    I have a question which i think is relevant to this topic. Is there any application that read a .class file and convert it to .java?

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Calling method from .class file

    Yes, what you want is a Java decompiler.
    There are quite a few available if you search for them on the internet.
    here is one: "http://java.decompiler.free.fr"

    There are some limitations, so you need to check what it can and can't do, for example some may not convert import statements and probably ignore comments. Ok for simple projects but not advisable for complex code in my opinion.

  5. #5
    Junior Member shadihrr's Avatar
    Join Date
    Jan 2010
    Location
    home
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Calling method from .class file

    that was awesome.thanks!

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Calling method from .class file


  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling method from .class file

    Quote Originally Posted by JavaDaveUK View Post
    I think you need to understand how the compiler works, it creates .class files from source files .java, you actually cannot call a method from a .java file, but you can from the compiled file .class.
    So to answer your question, yes you can call a method from a class file, just make sure it resides on your classpath so the JVM you are running under can see it.

    Incidentally IDE's are effectively looking at the .class files to determine what methods are available, this is obviously hidden from the developer.
    This is quite an old thread, but i really would like some help in this matter, with an example, probably? Like, how to the class file method say 'fn1' in class file 'myjava.class' can be called from a method 'fn2' in another java file, say 'mynewjava.java'??

    Thanks in advance

Similar Threads

  1. CashRegister class- adding a method getItemCount
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 21st, 2010, 08:29 PM
  2. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  3. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM
  4. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM