Reputation: 11
So I have this constructor in my main class:
public class GCTest extends Program {
GCTest(int x, int y) {
int gcd = gcd(Math.abs(x), Math.abs(y));
int num = x / gcd;
int den = Math.abs(y) / gcd;
if (y < 0) num = -num;
}
}
and when I call the constructor in my void run(), when I compile it the cmd gives me the following error "Cannot determine the main class" but when I create a new java file that only has the constructor and I compile them using the .; command the program runs fine. What am I doing wrong? I'm kinda new in programming. Thanks in advance!
import java.lang.Runtime;
import acm.program.*;
public class GCTest extends Program {
GCTest(int x, int y){
int gcd = gcd(Math.abs(x), Math.abs(y));
int num = x / gcd;
int den = Math.abs(y) / gcd;
if (y < 0) num = -num;
}
public int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b,a%b);
}
public void run() {
Runtime myRuntime = Runtime.getRuntime();
println("Allocating 10000 Rational objects.");
for(int i = 0; i < 10000; i++){
new GCTest(i + 1, i + 2);
}
long fB = myRuntime.freeMemory();
println("Free memory before garbage collection = " + fB);
myRuntime.gc();
long fA = myRuntime.freeMemory();
println("Free memory after garbage collection = " + fA);
println("Garbage collection freed up " + (fA - fB) + " bytes.");
}
}
When I compile and run the posted program I get the "cant determine main class error". When I compile my GCTest.java with this Rational.java (after I remove the constructor from the GCTest code and replace new GCTest with new Rational( , ) everything runs fine. This is the Rational.java :
public class Rational{
public Rational(int x, int y) {
int gcd = gcd(Math.abs(x), Math.abs(y));
int num = x / gcd;
int den = Math.abs(y) / gcd;
if (y < 0) num = -num;
}
public int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b,a%b);
}
}
Upvotes: 0
Views: 179
Reputation: 459
Here is an clarification:
The program posted by user3083578 is using acm.program package. You do not need to write your own main class if your class extends Program class. You only need to implement run() method your main class.
The documentation of the package can be found in here: http://www-cs-faculty.stanford.edu/~eroberts/jtf/rationale/ProgramPackage.html
You use javac -cp acm.jar GCTest.java
to compile the program.
After compiling, if you need to run your program, you can simply use
java GCTest
Update:
The correct way of running the program is
java -cp .:acm.jar GCTest
I can run the program if I take away the constructor you declared (and all the depended lines). I don't know if acm.package support declaring your own constructor. Also, I could not get .;
working and I think what you meant was .:
.
Upvotes: 1
Reputation: 4102
I assume that your Program class is an abstract class that implements the following line:
public static void main(String [] args) {
...
}
This is the entry point for your application. Without posting this code, I will guess that it then forces your extending class to implement the run
method. thus the main entry point calls run
on itself to kick off your program. Something like this
public abstract class Program {
public static void main(String[] args) {
this.run();
}
public abstract void run();
}
You are compiling with the following command:
javac -cp acm.jar GCTest.java
so you are telling the compiler to add acm.jar to your classpath, and ONLY compile GCTest.java
I am unsure with what you posted what your actual error is, but for now navigate to your source folder and run
javac -cp acm.jar *.java
and see if that helps
Upvotes: 1
Reputation: 715
To compile a java program you just have to pass that the program has no error.
But, to run your java program you must have to set an entry point to show the machine from where your program should run.
I hope you understand now what's happening for you now.
you must have a
public static void main(String[] args)
or
public static void main(String... args)
method in one of your class to set that entry point for your program.
Upvotes: 0
Reputation: 38511
You typically see this error when you forget to include the main method:
public static void main(String[] args)
in your program, which you did. Try something like this:
public class GCTest extends Program {
..
..
public static void main(String[] args) {
new GCTest(...).run();
}
}
main(..)
is a special method that tells Java where your program begins.
Upvotes: 2