Results 1 to 9 of 9
  1. #1
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default 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:
    Java 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:
    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'.
    Java 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);
    	}
    }
    Last edited by eviljeff; 12-28-2012 at 12:51 AM. Reason: Made the program simpler

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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

  3. #3
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default 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.

  4. #4
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default 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.

    Java 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.

  5. #5
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default 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.

  6. #6
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default 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.

  7. #7
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default Re: Run-time Error: Could not find the main class...

    Quote Originally Posted by Wizard0860 View Post
    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!

  8. #8
    eviljeff is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, Illinois, USA
    Posts
    19
    Rep Power
    0

    Default 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.
    Last edited by eviljeff; 12-28-2012 at 03:12 AM.

  9. #9
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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."

Similar Threads

  1. Error: Could not find or load main class
    By Tybald in forum Eclipse
    Replies: 5
    Last Post: 05-28-2012, 11:22 AM
  2. Could not find the main class error
    By jasper580 in forum New To Java
    Replies: 12
    Last Post: 03-08-2012, 06:02 PM
  3. Replies: 3
    Last Post: 12-09-2011, 03:51 AM
  4. runtime error 'could not find main class' ?
    By ruud00000 in forum Java Applets
    Replies: 5
    Last Post: 12-31-2010, 05:58 PM
  5. [SOLVED] Could not find the main class: error...
    By bobleny in forum New To Java
    Replies: 21
    Last Post: 03-02-2010, 10:53 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
  •