Run-time Error: Could not find the main class...
I'm not exactly sure what the problem is. Both the class definition and the Java program that makes an object of class compile without errors, but I get the following errors when running the Java program:
Code:
Exception in thread "main" java.lang.UnsupportedClassVersionError: SpeciesSecondTryDemo : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: SpeciesSecondTryDemo. Program will exit.
Here is the class definition, called SpeciesSecondTry.java:
Code:
import java.util.Scanner;
public class SpeciesSecondTry
{
public String name;
public int population;
public double growthRate;
public void readInput()
{
Scanner kbd = new Scanner(System.in);
System.out.println("What is the species' name?");
name = kbd.nextLine();
System.out.println("What is the population of the species?");
population = kbd.nextInt();
System.out.println("Enter growth rate " + "(% increase per year): ");
growthRate = kbd.nextDouble();
}
public void writeOutput()
{
System.out.println("Name = " + name);
System.out.println("Population = " + population);
System.out.println("Growth rate = " + growthRate + "%");
}
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while ((count > 0) && (populationAmount > 0))
{
populationAmount = (populationAmount + (growthRate / 100) * populationAmount);
count--;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
public double getDensity(double area)
{
double density = population / area;
return density;
}
}
Now here is the Java program I'm trying to run, which contains an object and methods from the above class definition. Note the object entitled speciesOTM is a shortened form of 'speciesOfTheMonth'.
Code:
public class SpeciesSecondTryDemo
{
public static void main(String[] args)
{
SpeciesSecondTry speciesOTM = new SpeciesSecondTry();
System.out.println("Enter the data of the species of the month: ");
speciesOTM.readInput();
speciesOTM.writeOutput();
int popIn10 = speciesOTM.predictPopulation(10);
System.out.println("In ten years, the predicted population will is " + popIn10);
double density = speciesOTM.getDensity(5);
System.out.println("The density of species within 5 square miles is " + density);
}
}
Re: Run-time Error: Could not find the main class...
How are you trying to run the program? I ran it and it seemed to work fine:
Enter the data of the species of the month:
What is the species' name?
abc
What is the population of the species?
88
Enter growth rate (% increase per year):
0
Name = abc
Population = 88
Growth rate = 0.0%
In ten years, the predicted population will is 88
The density of species within 5 square miles is 17.6
Re: Run-time Error: Could not find the main class...
It seems to work fine for me.
Enter the data of the species of the month:
What is the species' name?
Me
What is the population of the species?
100
Enter growth rate (% increase per year):
10
Name = Me
Population = 100
Growth rate = 10.0%
In ten years, the predicted population will is 259
The density of species within 5 square miles is 20.0
Are you sure you are running the program and not doing something else?
ARE BOTH THE FILES SAVED IN THE SAME LOCATION? this could lead to errors.
Re: Run-time Error: Could not find the main class...
Hello Wizard, thanks for your reply. I'm not sure what's going wrong. I have all files relevant to the program contained in the same directory.
Code:
Exception in thread "main" java.lang.UnsupportedClassVersionError: SpeciesSecondTryDemo : Unsupported major.minor version 51.0
I'm not sure what that means. I will continue to try to find something helpful via Google.
Re: Run-time Error: Could not find the main class...
By the way, I use an IDE/Text Editor called Geany, and it compiles and runs Java programs. However, on the command line I use 'javac SpeciesSecondTry.java' and 'javac SpeciesSecondTryDemo.java' to compile, and then 'java SpeciesSecondTryDemo' to run. Both ways result in the same error message.
Re: Run-time Error: Could not find the main class...
I believe it means you have compiled with a newer version of java than you are running it on. So you may have compiled with Java 7, but your JRE is java 6.
Other than that, I really can't see a problem in your coding, and the text editor you use shouldn't really make a difference. I personally am some of the few who use jgrasp, but that really doesn't matter.
something useful to look at may be
How do I resolve a java.lang.UnsupportedClassVersionError?
Hopefully i was helpful.
Re: Run-time Error: Could not find the main class...
Quote:
Originally Posted by
Wizard0860
I believe it means you have compiled with a newer version of java than you are running it on. So you may have compiled with Java 7, but your JRE is java 6.
Other than that, I really can't see a problem in your coding, and the text editor you use shouldn't really make a difference. I personally am some of the few who use jgrasp, but that really doesn't matter.
something useful to look at may be
How do I resolve a java.lang.UnsupportedClassVersionError?
Hopefully i was helpful.
Hello Wizard. Yes, I figured it may have been something to do with the version of java and javac. I did a version check 'javac -version' and 'java -version' and discovered that they were indeed different. I had installed openjdk-7-jdk a while ago, but apparently it wasn't the version being used. Thanks for your help!
Re: Run-time Error: Could not find the main class...
By the way, I use jGrasp myself when I'm on Windows. I personally like it.
But the reason I mentioned the IDE in my other post was because Andrew wanted to know how I am trying to run the program.
Re: Run-time Error: Could not find the main class...
Also,
UnsupportedClassVersionError (Java 2 Platform SE v1.4.2)
"Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported."