Results 1 to 6 of 6
- 10-16-2010, 03:35 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
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?
And this is the error I am getting: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); }
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?
-
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?:
It will return an int but you appear to be discarding this return value making it a wasted method call.Java Code:myRuntime.freememory();
LuckLast edited by Fubarable; 10-16-2010 at 03:42 PM.
- 10-16-2010, 04:38 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
The method 'freeMemory' is camel cased too ...lesson: always read the API documentation first.
kind regards,
Jos
- 10-18-2010, 12:19 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
Sorted
Thank you guys,u have been helpful.I sorted the problem by your help.:D
- 10-18-2010, 12:24 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-18-2010, 06:48 PM #6
Similar Threads
-
Java/Ubuntu: Runtime.getRuntime().exec(..) gives an error
By t500yo in forum New To JavaReplies: 5Last Post: 07-07-2010, 12:50 PM -
Slow Output reading with Runtime.getRuntime().exec()
By gmcouto in forum Advanced JavaReplies: 8Last Post: 05-14-2010, 10:42 AM -
problem with Runtime.getRuntime().exec when running java in .bat
By Shayko in forum Threads and SynchronizationReplies: 2Last Post: 01-27-2010, 07:46 PM -
help with Runtime.getRuntime().exec
By collin389 in forum AWT / SwingReplies: 3Last Post: 11-09-2009, 04:22 AM -
grep on multiple files using Runtime.getRuntime().exec()
By cprash.aggarwal in forum Advanced JavaReplies: 3Last Post: 02-11-2009, 06:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks