Results 1 to 6 of 6
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Why can I use Runtime.getRuntime() in Eclipse

    Hi Folks,
    I am using Eclipse and I can't use Runtime.getRuntime() or freememory in order to make garbage collection program.Any ideas?

    Java Code:
    /*File:Rational.java
     * Write a program that allocates 10000 Rational objects without saving any of them
    in variables so that they all become garbage. Once you’ve done so, measure the
    amount of free memory before and after garbage collection and use the difference to
    report how many bytes were freed, as shown in the following sample run:
    */
     
    import acm.program.*;
    import java.util.*;
    import java.lang.*;
    
        
        
        
    
        
    import acm.util.*;
        /**
        * The Rational class is used to represent rational numbers, which
        * are defined to be the quotient of two integers.
        */
        public class Rational {
    
            
            private static int totalrational=1000;
     
            
            public static void main (String[]arg){
                
                
            
              Runtime myRuntime = new Runtime.getRuntime();
                
                myRuntime.freememory();
                
                for(int i=0; i<totalrational; i++){
                Rational a= new Rational(1, 2);
                Rational b = new Rational(1, 3);
                Rational c = new Rational(1, 6);
                Rational sum = a.add(b).add(c);
                System.out.println(a + " + " + b + " + " + c + " = " + sum);
                }
        
                
            }
        /**
        * Creates a new Rational initialized to zero.
        */
        public Rational() {
        this(0);
        }
        /**
        * Creates a new Rational from the integer argument.
        * @param n The initial value
        */
        public Rational(int n) {
        this(n, 1);
        }
    And this is the error I am getting:

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        Runtime.getRuntime cannot be resolved to a type
        The method freememory() is undefined for the type Runtime
    
        at Rational.main(Rational.java:33)

    According to a website we can get this kind of error if appropriate import is missing at the top.is that right?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This has nothing to do with Eclipse and nothing to do with imports (since Runtime is part of the java.lang package and available to all) but all to do with calling getRuntime() incorrectly. This is a static method not a constructor. There's no place for "new" here.

    Also, what is this supposed to do?:
    Java Code:
    myRuntime.freememory();
    It will return an int but you appear to be discarding this return value making it a wasted method call.

    Luck
    Last edited by Fubarable; 10-16-2010 at 04:42 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    The method 'freeMemory' is camel cased too ...lesson: always read the API documentation first.

    kind regards,

    Jos

  4. #4
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Sorted

    Thank you guys,u have been helpful.I sorted the problem by your help.:D

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ccie007 View Post
    Thank you guys,u have been helpful.I sorted the problem by your help.:D
    So it were all compilation problems and you could've found a solution by actually reading what the compiler had to say. It had totally nothing to do with Eclipse. When will people actually read diagnostic messages or the API documentation? It sure would lower the traffic on all the forums ...

    kind regards,

    Jos

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    When will people actually read diagnostic messages or the API documentation? It sure would lower the traffic on all the forums ...
    But then what would we spend our spare time doing? ;)

Similar Threads

  1. Replies: 5
    Last Post: 07-07-2010, 01:50 PM
  2. Slow Output reading with Runtime.getRuntime().exec()
    By gmcouto in forum Advanced Java
    Replies: 8
    Last Post: 05-14-2010, 11:42 AM
  3. problem with Runtime.getRuntime().exec when running java in .bat
    By Shayko in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 08:46 PM
  4. help with Runtime.getRuntime().exec
    By collin389 in forum AWT / Swing
    Replies: 3
    Last Post: 11-09-2009, 05:22 AM
  5. grep on multiple files using Runtime.getRuntime().exec()
    By cprash.aggarwal in forum Advanced Java
    Replies: 3
    Last Post: 02-11-2009, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •